Steve Hardy | 5812f92 | 2008-01-22 23:00:02 +0000 | [diff] [blame] | 1 | /* |
Guenter Roeck | d13d623 | 2012-01-19 11:02:14 -0800 | [diff] [blame] | 2 | * ads7828.c - lm_sensors driver for ads7828 12-bit 8-channel ADC |
| 3 | * (C) 2007 EADS Astrium |
| 4 | * |
| 5 | * This driver is based on the lm75 and other lm_sensors/hwmon drivers |
| 6 | * |
| 7 | * Written by Steve Hardy <shardy@redhat.com> |
| 8 | * |
Vivien Didelot | 46d7846 | 2012-10-03 16:54:07 -0400 | [diff] [blame^] | 9 | * For further information, see the Documentation/hwmon/ads7828 file. |
Guenter Roeck | d13d623 | 2012-01-19 11:02:14 -0800 | [diff] [blame] | 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 24 | */ |
Steve Hardy | 5812f92 | 2008-01-22 23:00:02 +0000 | [diff] [blame] | 25 | |
Vivien Didelot | 46d7846 | 2012-10-03 16:54:07 -0400 | [diff] [blame^] | 26 | #include <linux/err.h> |
Steve Hardy | 5812f92 | 2008-01-22 23:00:02 +0000 | [diff] [blame] | 27 | #include <linux/hwmon.h> |
| 28 | #include <linux/hwmon-sysfs.h> |
Vivien Didelot | 46d7846 | 2012-10-03 16:54:07 -0400 | [diff] [blame^] | 29 | #include <linux/i2c.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/jiffies.h> |
| 32 | #include <linux/module.h> |
Steve Hardy | 5812f92 | 2008-01-22 23:00:02 +0000 | [diff] [blame] | 33 | #include <linux/mutex.h> |
Vivien Didelot | 46d7846 | 2012-10-03 16:54:07 -0400 | [diff] [blame^] | 34 | #include <linux/platform_data/ads7828.h> |
| 35 | #include <linux/slab.h> |
Steve Hardy | 5812f92 | 2008-01-22 23:00:02 +0000 | [diff] [blame] | 36 | |
| 37 | /* The ADS7828 registers */ |
Vivien Didelot | 46d7846 | 2012-10-03 16:54:07 -0400 | [diff] [blame^] | 38 | #define ADS7828_NCH 8 /* 8 channels supported */ |
| 39 | #define ADS7828_CMD_SD_SE 0x80 /* Single ended inputs */ |
| 40 | #define ADS7828_CMD_PD1 0x04 /* Internal vref OFF && A/D ON */ |
| 41 | #define ADS7828_CMD_PD3 0x0C /* Internal vref ON && A/D ON */ |
| 42 | #define ADS7828_INT_VREF_MV 2500 /* Internal vref is 2.5V, 2500mV */ |
| 43 | #define ADS7828_EXT_VREF_MV_MIN 50 /* External vref min value 0.05V */ |
| 44 | #define ADS7828_EXT_VREF_MV_MAX 5250 /* External vref max value 5.25V */ |
Steve Hardy | 5812f92 | 2008-01-22 23:00:02 +0000 | [diff] [blame] | 45 | |
Vivien Didelot | 46d7846 | 2012-10-03 16:54:07 -0400 | [diff] [blame^] | 46 | /* Client specific data */ |
Steve Hardy | 5812f92 | 2008-01-22 23:00:02 +0000 | [diff] [blame] | 47 | struct ads7828_data { |
Steve Hardy | 5812f92 | 2008-01-22 23:00:02 +0000 | [diff] [blame] | 48 | struct device *hwmon_dev; |
Vivien Didelot | 46d7846 | 2012-10-03 16:54:07 -0400 | [diff] [blame^] | 49 | struct mutex update_lock; /* Mutex protecting updates */ |
| 50 | unsigned long last_updated; /* Last updated time (in jiffies) */ |
| 51 | u16 adc_input[ADS7828_NCH]; /* ADS7828_NCH samples */ |
| 52 | bool valid; /* Validity flag */ |
| 53 | bool diff_input; /* Differential input */ |
| 54 | bool ext_vref; /* External voltage reference */ |
| 55 | unsigned int vref_mv; /* voltage reference value */ |
| 56 | u8 cmd_byte; /* Command byte without channel bits */ |
| 57 | unsigned int lsb_resol; /* Resolution of the ADC sample LSB */ |
Steve Hardy | 5812f92 | 2008-01-22 23:00:02 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
Vivien Didelot | 46d7846 | 2012-10-03 16:54:07 -0400 | [diff] [blame^] | 60 | /* Command byte C2,C1,C0 - see datasheet */ |
| 61 | static inline u8 ads7828_cmd_byte(u8 cmd, int ch) |
Steve Hardy | 5812f92 | 2008-01-22 23:00:02 +0000 | [diff] [blame] | 62 | { |
Vivien Didelot | 46d7846 | 2012-10-03 16:54:07 -0400 | [diff] [blame^] | 63 | return cmd | (((ch >> 1) | (ch & 0x01) << 2) << 4); |
Steve Hardy | 5812f92 | 2008-01-22 23:00:02 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /* Update data for the device (all 8 channels) */ |
| 67 | static struct ads7828_data *ads7828_update_device(struct device *dev) |
| 68 | { |
| 69 | struct i2c_client *client = to_i2c_client(dev); |
| 70 | struct ads7828_data *data = i2c_get_clientdata(client); |
| 71 | |
| 72 | mutex_lock(&data->update_lock); |
| 73 | |
| 74 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) |
| 75 | || !data->valid) { |
| 76 | unsigned int ch; |
| 77 | dev_dbg(&client->dev, "Starting ads7828 update\n"); |
| 78 | |
| 79 | for (ch = 0; ch < ADS7828_NCH; ch++) { |
Vivien Didelot | 46d7846 | 2012-10-03 16:54:07 -0400 | [diff] [blame^] | 80 | u8 cmd = ads7828_cmd_byte(data->cmd_byte, ch); |
Jean Delvare | 90f4102 | 2011-11-04 12:00:47 +0100 | [diff] [blame] | 81 | data->adc_input[ch] = |
| 82 | i2c_smbus_read_word_swapped(client, cmd); |
Steve Hardy | 5812f92 | 2008-01-22 23:00:02 +0000 | [diff] [blame] | 83 | } |
| 84 | data->last_updated = jiffies; |
Vivien Didelot | 46d7846 | 2012-10-03 16:54:07 -0400 | [diff] [blame^] | 85 | data->valid = true; |
Steve Hardy | 5812f92 | 2008-01-22 23:00:02 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | mutex_unlock(&data->update_lock); |
| 89 | |
| 90 | return data; |
| 91 | } |
| 92 | |
| 93 | /* sysfs callback function */ |
Vivien Didelot | 46d7846 | 2012-10-03 16:54:07 -0400 | [diff] [blame^] | 94 | static ssize_t ads7828_show_in(struct device *dev, struct device_attribute *da, |
| 95 | char *buf) |
Steve Hardy | 5812f92 | 2008-01-22 23:00:02 +0000 | [diff] [blame] | 96 | { |
| 97 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
| 98 | struct ads7828_data *data = ads7828_update_device(dev); |
Vivien Didelot | 46d7846 | 2012-10-03 16:54:07 -0400 | [diff] [blame^] | 99 | unsigned int value = DIV_ROUND_CLOSEST(data->adc_input[attr->index] * |
| 100 | data->lsb_resol, 1000); |
| 101 | |
| 102 | return sprintf(buf, "%d\n", value); |
Steve Hardy | 5812f92 | 2008-01-22 23:00:02 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Vivien Didelot | 46d7846 | 2012-10-03 16:54:07 -0400 | [diff] [blame^] | 105 | static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, ads7828_show_in, NULL, 0); |
| 106 | static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ads7828_show_in, NULL, 1); |
| 107 | static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, ads7828_show_in, NULL, 2); |
| 108 | static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, ads7828_show_in, NULL, 3); |
| 109 | static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, ads7828_show_in, NULL, 4); |
| 110 | static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, ads7828_show_in, NULL, 5); |
| 111 | static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, ads7828_show_in, NULL, 6); |
| 112 | static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, ads7828_show_in, NULL, 7); |
Steve Hardy | 5812f92 | 2008-01-22 23:00:02 +0000 | [diff] [blame] | 113 | |
| 114 | static struct attribute *ads7828_attributes[] = { |
| 115 | &sensor_dev_attr_in0_input.dev_attr.attr, |
| 116 | &sensor_dev_attr_in1_input.dev_attr.attr, |
| 117 | &sensor_dev_attr_in2_input.dev_attr.attr, |
| 118 | &sensor_dev_attr_in3_input.dev_attr.attr, |
| 119 | &sensor_dev_attr_in4_input.dev_attr.attr, |
| 120 | &sensor_dev_attr_in5_input.dev_attr.attr, |
| 121 | &sensor_dev_attr_in6_input.dev_attr.attr, |
| 122 | &sensor_dev_attr_in7_input.dev_attr.attr, |
| 123 | NULL |
| 124 | }; |
| 125 | |
| 126 | static const struct attribute_group ads7828_group = { |
| 127 | .attrs = ads7828_attributes, |
| 128 | }; |
| 129 | |
Jean Delvare | 7347cb3 | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 130 | static int ads7828_remove(struct i2c_client *client) |
Steve Hardy | 5812f92 | 2008-01-22 23:00:02 +0000 | [diff] [blame] | 131 | { |
| 132 | struct ads7828_data *data = i2c_get_clientdata(client); |
Vivien Didelot | 46d7846 | 2012-10-03 16:54:07 -0400 | [diff] [blame^] | 133 | |
Steve Hardy | 5812f92 | 2008-01-22 23:00:02 +0000 | [diff] [blame] | 134 | hwmon_device_unregister(data->hwmon_dev); |
| 135 | sysfs_remove_group(&client->dev.kobj, &ads7828_group); |
Steve Hardy | 5812f92 | 2008-01-22 23:00:02 +0000 | [diff] [blame] | 136 | |
Jean Delvare | 7347cb3 | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 137 | return 0; |
| 138 | } |
Steve Hardy | 5812f92 | 2008-01-22 23:00:02 +0000 | [diff] [blame] | 139 | |
Jean Delvare | 7347cb3 | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 140 | static int ads7828_probe(struct i2c_client *client, |
| 141 | const struct i2c_device_id *id) |
| 142 | { |
Vivien Didelot | 46d7846 | 2012-10-03 16:54:07 -0400 | [diff] [blame^] | 143 | struct ads7828_platform_data *pdata = client->dev.platform_data; |
Jean Delvare | 7347cb3 | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 144 | struct ads7828_data *data; |
| 145 | int err; |
Steve Hardy | 5812f92 | 2008-01-22 23:00:02 +0000 | [diff] [blame] | 146 | |
Guenter Roeck | 34e3f7f | 2012-06-02 09:58:00 -0700 | [diff] [blame] | 147 | data = devm_kzalloc(&client->dev, sizeof(struct ads7828_data), |
| 148 | GFP_KERNEL); |
| 149 | if (!data) |
| 150 | return -ENOMEM; |
Steve Hardy | 5812f92 | 2008-01-22 23:00:02 +0000 | [diff] [blame] | 151 | |
Vivien Didelot | 46d7846 | 2012-10-03 16:54:07 -0400 | [diff] [blame^] | 152 | if (pdata) { |
| 153 | data->diff_input = pdata->diff_input; |
| 154 | data->ext_vref = pdata->ext_vref; |
| 155 | if (data->ext_vref) |
| 156 | data->vref_mv = pdata->vref_mv; |
| 157 | } |
| 158 | |
| 159 | /* Bound Vref with min/max values if it was provided */ |
| 160 | if (data->vref_mv) |
| 161 | data->vref_mv = SENSORS_LIMIT(data->vref_mv, |
| 162 | ADS7828_EXT_VREF_MV_MIN, |
| 163 | ADS7828_EXT_VREF_MV_MAX); |
| 164 | else |
| 165 | data->vref_mv = ADS7828_INT_VREF_MV; |
| 166 | |
| 167 | data->lsb_resol = DIV_ROUND_CLOSEST(data->vref_mv * 1000, 4096); |
| 168 | |
| 169 | data->cmd_byte = data->ext_vref ? ADS7828_CMD_PD1 : ADS7828_CMD_PD3; |
| 170 | if (!data->diff_input) |
| 171 | data->cmd_byte |= ADS7828_CMD_SD_SE; |
| 172 | |
Jean Delvare | 7347cb3 | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 173 | i2c_set_clientdata(client, data); |
Steve Hardy | 5812f92 | 2008-01-22 23:00:02 +0000 | [diff] [blame] | 174 | mutex_init(&data->update_lock); |
| 175 | |
Steve Hardy | 5812f92 | 2008-01-22 23:00:02 +0000 | [diff] [blame] | 176 | err = sysfs_create_group(&client->dev.kobj, &ads7828_group); |
| 177 | if (err) |
Guenter Roeck | 34e3f7f | 2012-06-02 09:58:00 -0700 | [diff] [blame] | 178 | return err; |
Steve Hardy | 5812f92 | 2008-01-22 23:00:02 +0000 | [diff] [blame] | 179 | |
| 180 | data->hwmon_dev = hwmon_device_register(&client->dev); |
| 181 | if (IS_ERR(data->hwmon_dev)) { |
| 182 | err = PTR_ERR(data->hwmon_dev); |
Vivien Didelot | 46d7846 | 2012-10-03 16:54:07 -0400 | [diff] [blame^] | 183 | goto error; |
Steve Hardy | 5812f92 | 2008-01-22 23:00:02 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | return 0; |
| 187 | |
Vivien Didelot | 46d7846 | 2012-10-03 16:54:07 -0400 | [diff] [blame^] | 188 | error: |
Steve Hardy | 5812f92 | 2008-01-22 23:00:02 +0000 | [diff] [blame] | 189 | sysfs_remove_group(&client->dev.kobj, &ads7828_group); |
Steve Hardy | 5812f92 | 2008-01-22 23:00:02 +0000 | [diff] [blame] | 190 | return err; |
| 191 | } |
| 192 | |
Vivien Didelot | 46d7846 | 2012-10-03 16:54:07 -0400 | [diff] [blame^] | 193 | static const struct i2c_device_id ads7828_device_ids[] = { |
| 194 | { "ads7828", 0 }, |
| 195 | { } |
| 196 | }; |
| 197 | MODULE_DEVICE_TABLE(i2c, ads7828_device_ids); |
Steve Hardy | 5812f92 | 2008-01-22 23:00:02 +0000 | [diff] [blame] | 198 | |
Vivien Didelot | 46d7846 | 2012-10-03 16:54:07 -0400 | [diff] [blame^] | 199 | static struct i2c_driver ads7828_driver = { |
| 200 | .driver = { |
| 201 | .name = "ads7828", |
| 202 | }, |
Steve Hardy | 5812f92 | 2008-01-22 23:00:02 +0000 | [diff] [blame] | 203 | |
Vivien Didelot | 46d7846 | 2012-10-03 16:54:07 -0400 | [diff] [blame^] | 204 | .id_table = ads7828_device_ids, |
| 205 | .probe = ads7828_probe, |
| 206 | .remove = ads7828_remove, |
| 207 | }; |
Steve Hardy | 5812f92 | 2008-01-22 23:00:02 +0000 | [diff] [blame] | 208 | |
Vivien Didelot | 46d7846 | 2012-10-03 16:54:07 -0400 | [diff] [blame^] | 209 | module_i2c_driver(ads7828_driver); |
Steve Hardy | 5812f92 | 2008-01-22 23:00:02 +0000 | [diff] [blame] | 210 | |
Steve Hardy | 5812f92 | 2008-01-22 23:00:02 +0000 | [diff] [blame] | 211 | MODULE_LICENSE("GPL"); |
Vivien Didelot | 46d7846 | 2012-10-03 16:54:07 -0400 | [diff] [blame^] | 212 | MODULE_AUTHOR("Steve Hardy <shardy@redhat.com>"); |
| 213 | MODULE_DESCRIPTION("Driver for TI ADS7828 A/D converter"); |