diff options
Diffstat (limited to 'drivers/hwmon/ads7828.c')
-rw-r--r-- | drivers/hwmon/ads7828.c | 102 |
1 files changed, 40 insertions, 62 deletions
diff --git a/drivers/hwmon/ads7828.c b/drivers/hwmon/ads7828.c index a622d40eec17..bce4e9ff21bf 100644 --- a/drivers/hwmon/ads7828.c +++ b/drivers/hwmon/ads7828.c | |||
@@ -30,14 +30,12 @@ | |||
30 | #include <linux/hwmon-sysfs.h> | 30 | #include <linux/hwmon-sysfs.h> |
31 | #include <linux/i2c.h> | 31 | #include <linux/i2c.h> |
32 | #include <linux/init.h> | 32 | #include <linux/init.h> |
33 | #include <linux/jiffies.h> | ||
34 | #include <linux/module.h> | 33 | #include <linux/module.h> |
35 | #include <linux/mutex.h> | ||
36 | #include <linux/platform_data/ads7828.h> | 34 | #include <linux/platform_data/ads7828.h> |
35 | #include <linux/regmap.h> | ||
37 | #include <linux/slab.h> | 36 | #include <linux/slab.h> |
38 | 37 | ||
39 | /* The ADS7828 registers */ | 38 | /* The ADS7828 registers */ |
40 | #define ADS7828_NCH 8 /* 8 channels supported */ | ||
41 | #define ADS7828_CMD_SD_SE 0x80 /* Single ended inputs */ | 39 | #define ADS7828_CMD_SD_SE 0x80 /* Single ended inputs */ |
42 | #define ADS7828_CMD_PD1 0x04 /* Internal vref OFF && A/D ON */ | 40 | #define ADS7828_CMD_PD1 0x04 /* Internal vref OFF && A/D ON */ |
43 | #define ADS7828_CMD_PD3 0x0C /* Internal vref ON && A/D ON */ | 41 | #define ADS7828_CMD_PD3 0x0C /* Internal vref ON && A/D ON */ |
@@ -50,17 +48,9 @@ enum ads7828_chips { ads7828, ads7830 }; | |||
50 | 48 | ||
51 | /* Client specific data */ | 49 | /* Client specific data */ |
52 | struct ads7828_data { | 50 | struct ads7828_data { |
53 | struct i2c_client *client; | 51 | struct regmap *regmap; |
54 | struct mutex update_lock; /* Mutex protecting updates */ | ||
55 | unsigned long last_updated; /* Last updated time (in jiffies) */ | ||
56 | u16 adc_input[ADS7828_NCH]; /* ADS7828_NCH samples */ | ||
57 | bool valid; /* Validity flag */ | ||
58 | bool diff_input; /* Differential input */ | ||
59 | bool ext_vref; /* External voltage reference */ | ||
60 | unsigned int vref_mv; /* voltage reference value */ | ||
61 | u8 cmd_byte; /* Command byte without channel bits */ | 52 | u8 cmd_byte; /* Command byte without channel bits */ |
62 | unsigned int lsb_resol; /* Resolution of the ADC sample LSB */ | 53 | unsigned int lsb_resol; /* Resolution of the ADC sample LSB */ |
63 | s32 (*read_channel)(const struct i2c_client *client, u8 command); | ||
64 | }; | 54 | }; |
65 | 55 | ||
66 | /* Command byte C2,C1,C0 - see datasheet */ | 56 | /* Command byte C2,C1,C0 - see datasheet */ |
@@ -69,42 +59,22 @@ static inline u8 ads7828_cmd_byte(u8 cmd, int ch) | |||
69 | return cmd | (((ch >> 1) | (ch & 0x01) << 2) << 4); | 59 | return cmd | (((ch >> 1) | (ch & 0x01) << 2) << 4); |
70 | } | 60 | } |
71 | 61 | ||
72 | /* Update data for the device (all 8 channels) */ | ||
73 | static struct ads7828_data *ads7828_update_device(struct device *dev) | ||
74 | { | ||
75 | struct ads7828_data *data = dev_get_drvdata(dev); | ||
76 | struct i2c_client *client = data->client; | ||
77 | |||
78 | mutex_lock(&data->update_lock); | ||
79 | |||
80 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) | ||
81 | || !data->valid) { | ||
82 | unsigned int ch; | ||
83 | dev_dbg(&client->dev, "Starting ads7828 update\n"); | ||
84 | |||
85 | for (ch = 0; ch < ADS7828_NCH; ch++) { | ||
86 | u8 cmd = ads7828_cmd_byte(data->cmd_byte, ch); | ||
87 | data->adc_input[ch] = data->read_channel(client, cmd); | ||
88 | } | ||
89 | data->last_updated = jiffies; | ||
90 | data->valid = true; | ||
91 | } | ||
92 | |||
93 | mutex_unlock(&data->update_lock); | ||
94 | |||
95 | return data; | ||
96 | } | ||
97 | |||
98 | /* sysfs callback function */ | 62 | /* sysfs callback function */ |
99 | static ssize_t ads7828_show_in(struct device *dev, struct device_attribute *da, | 63 | static ssize_t ads7828_show_in(struct device *dev, struct device_attribute *da, |
100 | char *buf) | 64 | char *buf) |
101 | { | 65 | { |
102 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); | 66 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
103 | struct ads7828_data *data = ads7828_update_device(dev); | 67 | struct ads7828_data *data = dev_get_drvdata(dev); |
104 | unsigned int value = DIV_ROUND_CLOSEST(data->adc_input[attr->index] * | 68 | u8 cmd = ads7828_cmd_byte(data->cmd_byte, attr->index); |
105 | data->lsb_resol, 1000); | 69 | unsigned int regval; |
70 | int err; | ||
106 | 71 | ||
107 | return sprintf(buf, "%d\n", value); | 72 | err = regmap_read(data->regmap, cmd, ®val); |
73 | if (err < 0) | ||
74 | return err; | ||
75 | |||
76 | return sprintf(buf, "%d\n", | ||
77 | DIV_ROUND_CLOSEST(regval * data->lsb_resol, 1000)); | ||
108 | } | 78 | } |
109 | 79 | ||
110 | static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, ads7828_show_in, NULL, 0); | 80 | static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, ads7828_show_in, NULL, 0); |
@@ -130,6 +100,16 @@ static struct attribute *ads7828_attrs[] = { | |||
130 | 100 | ||
131 | ATTRIBUTE_GROUPS(ads7828); | 101 | ATTRIBUTE_GROUPS(ads7828); |
132 | 102 | ||
103 | static const struct regmap_config ads2828_regmap_config = { | ||
104 | .reg_bits = 8, | ||
105 | .val_bits = 16, | ||
106 | }; | ||
107 | |||
108 | static const struct regmap_config ads2830_regmap_config = { | ||
109 | .reg_bits = 8, | ||
110 | .val_bits = 8, | ||
111 | }; | ||
112 | |||
133 | static int ads7828_probe(struct i2c_client *client, | 113 | static int ads7828_probe(struct i2c_client *client, |
134 | const struct i2c_device_id *id) | 114 | const struct i2c_device_id *id) |
135 | { | 115 | { |
@@ -137,42 +117,40 @@ static int ads7828_probe(struct i2c_client *client, | |||
137 | struct ads7828_platform_data *pdata = dev_get_platdata(dev); | 117 | struct ads7828_platform_data *pdata = dev_get_platdata(dev); |
138 | struct ads7828_data *data; | 118 | struct ads7828_data *data; |
139 | struct device *hwmon_dev; | 119 | struct device *hwmon_dev; |
120 | unsigned int vref_mv = ADS7828_INT_VREF_MV; | ||
121 | bool diff_input = false; | ||
122 | bool ext_vref = false; | ||
140 | 123 | ||
141 | data = devm_kzalloc(dev, sizeof(struct ads7828_data), GFP_KERNEL); | 124 | data = devm_kzalloc(dev, sizeof(struct ads7828_data), GFP_KERNEL); |
142 | if (!data) | 125 | if (!data) |
143 | return -ENOMEM; | 126 | return -ENOMEM; |
144 | 127 | ||
145 | if (pdata) { | 128 | if (pdata) { |
146 | data->diff_input = pdata->diff_input; | 129 | diff_input = pdata->diff_input; |
147 | data->ext_vref = pdata->ext_vref; | 130 | ext_vref = pdata->ext_vref; |
148 | if (data->ext_vref) | 131 | if (ext_vref && pdata->vref_mv) |
149 | data->vref_mv = pdata->vref_mv; | 132 | vref_mv = pdata->vref_mv; |
150 | } | 133 | } |
151 | 134 | ||
152 | /* Bound Vref with min/max values if it was provided */ | 135 | /* Bound Vref with min/max values */ |
153 | if (data->vref_mv) | 136 | vref_mv = clamp_val(vref_mv, ADS7828_EXT_VREF_MV_MIN, |
154 | data->vref_mv = clamp_val(data->vref_mv, | 137 | ADS7828_EXT_VREF_MV_MAX); |
155 | ADS7828_EXT_VREF_MV_MIN, | ||
156 | ADS7828_EXT_VREF_MV_MAX); | ||
157 | else | ||
158 | data->vref_mv = ADS7828_INT_VREF_MV; | ||
159 | 138 | ||
160 | /* ADS7828 uses 12-bit samples, while ADS7830 is 8-bit */ | 139 | /* ADS7828 uses 12-bit samples, while ADS7830 is 8-bit */ |
161 | if (id->driver_data == ads7828) { | 140 | if (id->driver_data == ads7828) { |
162 | data->lsb_resol = DIV_ROUND_CLOSEST(data->vref_mv * 1000, 4096); | 141 | data->lsb_resol = DIV_ROUND_CLOSEST(vref_mv * 1000, 4096); |
163 | data->read_channel = i2c_smbus_read_word_swapped; | 142 | data->regmap = devm_regmap_init_i2c(client, |
143 | &ads2828_regmap_config); | ||
164 | } else { | 144 | } else { |
165 | data->lsb_resol = DIV_ROUND_CLOSEST(data->vref_mv * 1000, 256); | 145 | data->lsb_resol = DIV_ROUND_CLOSEST(vref_mv * 1000, 256); |
166 | data->read_channel = i2c_smbus_read_byte_data; | 146 | data->regmap = devm_regmap_init_i2c(client, |
147 | &ads2830_regmap_config); | ||
167 | } | 148 | } |
168 | 149 | ||
169 | data->cmd_byte = data->ext_vref ? ADS7828_CMD_PD1 : ADS7828_CMD_PD3; | 150 | data->cmd_byte = ext_vref ? ADS7828_CMD_PD1 : ADS7828_CMD_PD3; |
170 | if (!data->diff_input) | 151 | if (!diff_input) |
171 | data->cmd_byte |= ADS7828_CMD_SD_SE; | 152 | data->cmd_byte |= ADS7828_CMD_SD_SE; |
172 | 153 | ||
173 | data->client = client; | ||
174 | mutex_init(&data->update_lock); | ||
175 | |||
176 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, | 154 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, |
177 | data, | 155 | data, |
178 | ads7828_groups); | 156 | ads7828_groups); |