Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 1 | /* |
| 2 | * emc1403.c - SMSC Thermal Driver |
| 3 | * |
| 4 | * Copyright (C) 2008 Intel Corp |
| 5 | * |
| 6 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; version 2 of the License. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 20 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 21 | * |
| 22 | * TODO |
| 23 | * - cache alarm and critical limit registers |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/i2c.h> |
| 30 | #include <linux/hwmon.h> |
| 31 | #include <linux/hwmon-sysfs.h> |
| 32 | #include <linux/err.h> |
| 33 | #include <linux/sysfs.h> |
| 34 | #include <linux/mutex.h> |
Jean Delvare | dcd8f39 | 2012-10-10 15:25:56 +0200 | [diff] [blame] | 35 | #include <linux/jiffies.h> |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 36 | |
| 37 | #define THERMAL_PID_REG 0xfd |
| 38 | #define THERMAL_SMSC_ID_REG 0xfe |
| 39 | #define THERMAL_REVISION_REG 0xff |
| 40 | |
Josef Gajdusek | be7f5c4 | 2014-05-12 14:34:09 +0200 | [diff] [blame^] | 41 | enum emc1403_chip { emc1402, emc1403, emc1404 }; |
| 42 | |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 43 | struct thermal_data { |
Guenter Roeck | 454aee1 | 2013-09-29 11:49:53 -0700 | [diff] [blame] | 44 | struct i2c_client *client; |
Josef Gajdusek | be7f5c4 | 2014-05-12 14:34:09 +0200 | [diff] [blame^] | 45 | const struct attribute_group *groups[4]; |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 46 | struct mutex mutex; |
Guenter Roeck | 4bebced | 2012-01-19 11:02:17 -0800 | [diff] [blame] | 47 | /* |
| 48 | * Cache the hyst value so we don't keep re-reading it. In theory |
| 49 | * we could cache it forever as nobody else should be writing it. |
| 50 | */ |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 51 | u8 cached_hyst; |
| 52 | unsigned long hyst_valid; |
| 53 | }; |
| 54 | |
| 55 | static ssize_t show_temp(struct device *dev, |
| 56 | struct device_attribute *attr, char *buf) |
| 57 | { |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 58 | struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); |
Guenter Roeck | 454aee1 | 2013-09-29 11:49:53 -0700 | [diff] [blame] | 59 | struct thermal_data *data = dev_get_drvdata(dev); |
| 60 | int retval; |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 61 | |
Guenter Roeck | 454aee1 | 2013-09-29 11:49:53 -0700 | [diff] [blame] | 62 | retval = i2c_smbus_read_byte_data(data->client, sda->index); |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 63 | if (retval < 0) |
| 64 | return retval; |
| 65 | return sprintf(buf, "%d000\n", retval); |
| 66 | } |
| 67 | |
| 68 | static ssize_t show_bit(struct device *dev, |
| 69 | struct device_attribute *attr, char *buf) |
| 70 | { |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 71 | struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr); |
Guenter Roeck | 454aee1 | 2013-09-29 11:49:53 -0700 | [diff] [blame] | 72 | struct thermal_data *data = dev_get_drvdata(dev); |
| 73 | int retval; |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 74 | |
Guenter Roeck | 454aee1 | 2013-09-29 11:49:53 -0700 | [diff] [blame] | 75 | retval = i2c_smbus_read_byte_data(data->client, sda->nr); |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 76 | if (retval < 0) |
| 77 | return retval; |
Guenter Roeck | 454aee1 | 2013-09-29 11:49:53 -0700 | [diff] [blame] | 78 | return sprintf(buf, "%d\n", !!(retval & sda->index)); |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static ssize_t store_temp(struct device *dev, |
| 82 | struct device_attribute *attr, const char *buf, size_t count) |
| 83 | { |
| 84 | struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); |
Guenter Roeck | 454aee1 | 2013-09-29 11:49:53 -0700 | [diff] [blame] | 85 | struct thermal_data *data = dev_get_drvdata(dev); |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 86 | unsigned long val; |
| 87 | int retval; |
| 88 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 89 | if (kstrtoul(buf, 10, &val)) |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 90 | return -EINVAL; |
Guenter Roeck | 454aee1 | 2013-09-29 11:49:53 -0700 | [diff] [blame] | 91 | retval = i2c_smbus_write_byte_data(data->client, sda->index, |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 92 | DIV_ROUND_CLOSEST(val, 1000)); |
| 93 | if (retval < 0) |
| 94 | return retval; |
| 95 | return count; |
| 96 | } |
| 97 | |
Alan Cox | 960f12f | 2010-08-14 21:08:49 +0200 | [diff] [blame] | 98 | static ssize_t store_bit(struct device *dev, |
| 99 | struct device_attribute *attr, const char *buf, size_t count) |
| 100 | { |
Alan Cox | 960f12f | 2010-08-14 21:08:49 +0200 | [diff] [blame] | 101 | struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr); |
Guenter Roeck | 454aee1 | 2013-09-29 11:49:53 -0700 | [diff] [blame] | 102 | struct thermal_data *data = dev_get_drvdata(dev); |
| 103 | struct i2c_client *client = data->client; |
Alan Cox | 960f12f | 2010-08-14 21:08:49 +0200 | [diff] [blame] | 104 | unsigned long val; |
| 105 | int retval; |
| 106 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 107 | if (kstrtoul(buf, 10, &val)) |
Alan Cox | 960f12f | 2010-08-14 21:08:49 +0200 | [diff] [blame] | 108 | return -EINVAL; |
| 109 | |
| 110 | mutex_lock(&data->mutex); |
| 111 | retval = i2c_smbus_read_byte_data(client, sda->nr); |
| 112 | if (retval < 0) |
| 113 | goto fail; |
| 114 | |
| 115 | retval &= ~sda->index; |
| 116 | if (val) |
| 117 | retval |= sda->index; |
| 118 | |
| 119 | retval = i2c_smbus_write_byte_data(client, sda->index, retval); |
| 120 | if (retval == 0) |
| 121 | retval = count; |
| 122 | fail: |
| 123 | mutex_unlock(&data->mutex); |
| 124 | return retval; |
| 125 | } |
| 126 | |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 127 | static ssize_t show_hyst(struct device *dev, |
| 128 | struct device_attribute *attr, char *buf) |
| 129 | { |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 130 | struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); |
Guenter Roeck | 454aee1 | 2013-09-29 11:49:53 -0700 | [diff] [blame] | 131 | struct thermal_data *data = dev_get_drvdata(dev); |
| 132 | struct i2c_client *client = data->client; |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 133 | int retval; |
| 134 | int hyst; |
| 135 | |
| 136 | retval = i2c_smbus_read_byte_data(client, sda->index); |
| 137 | if (retval < 0) |
| 138 | return retval; |
| 139 | |
| 140 | if (time_after(jiffies, data->hyst_valid)) { |
| 141 | hyst = i2c_smbus_read_byte_data(client, 0x21); |
| 142 | if (hyst < 0) |
| 143 | return retval; |
| 144 | data->cached_hyst = hyst; |
| 145 | data->hyst_valid = jiffies + HZ; |
| 146 | } |
| 147 | return sprintf(buf, "%d000\n", retval - data->cached_hyst); |
| 148 | } |
| 149 | |
| 150 | static ssize_t store_hyst(struct device *dev, |
| 151 | struct device_attribute *attr, const char *buf, size_t count) |
| 152 | { |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 153 | struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); |
Guenter Roeck | 454aee1 | 2013-09-29 11:49:53 -0700 | [diff] [blame] | 154 | struct thermal_data *data = dev_get_drvdata(dev); |
| 155 | struct i2c_client *client = data->client; |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 156 | int retval; |
| 157 | int hyst; |
| 158 | unsigned long val; |
| 159 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 160 | if (kstrtoul(buf, 10, &val)) |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 161 | return -EINVAL; |
| 162 | |
| 163 | mutex_lock(&data->mutex); |
| 164 | retval = i2c_smbus_read_byte_data(client, sda->index); |
| 165 | if (retval < 0) |
| 166 | goto fail; |
| 167 | |
Josef Gajdusek | 17c048f | 2014-05-11 14:40:44 +0200 | [diff] [blame] | 168 | hyst = retval * 1000 - val; |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 169 | hyst = DIV_ROUND_CLOSEST(hyst, 1000); |
| 170 | if (hyst < 0 || hyst > 255) { |
| 171 | retval = -ERANGE; |
| 172 | goto fail; |
| 173 | } |
| 174 | |
| 175 | retval = i2c_smbus_write_byte_data(client, 0x21, hyst); |
| 176 | if (retval == 0) { |
| 177 | retval = count; |
| 178 | data->cached_hyst = hyst; |
| 179 | data->hyst_valid = jiffies + HZ; |
| 180 | } |
| 181 | fail: |
| 182 | mutex_unlock(&data->mutex); |
| 183 | return retval; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * Sensors. We pass the actual i2c register to the methods. |
| 188 | */ |
| 189 | |
| 190 | static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO | S_IWUSR, |
| 191 | show_temp, store_temp, 0x06); |
| 192 | static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, |
| 193 | show_temp, store_temp, 0x05); |
| 194 | static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR, |
| 195 | show_temp, store_temp, 0x20); |
| 196 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0x00); |
| 197 | static SENSOR_DEVICE_ATTR_2(temp1_min_alarm, S_IRUGO, |
| 198 | show_bit, NULL, 0x36, 0x01); |
| 199 | static SENSOR_DEVICE_ATTR_2(temp1_max_alarm, S_IRUGO, |
| 200 | show_bit, NULL, 0x35, 0x01); |
| 201 | static SENSOR_DEVICE_ATTR_2(temp1_crit_alarm, S_IRUGO, |
| 202 | show_bit, NULL, 0x37, 0x01); |
| 203 | static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO | S_IWUSR, |
| 204 | show_hyst, store_hyst, 0x20); |
| 205 | |
| 206 | static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO | S_IWUSR, |
| 207 | show_temp, store_temp, 0x08); |
| 208 | static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR, |
| 209 | show_temp, store_temp, 0x07); |
| 210 | static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO | S_IWUSR, |
| 211 | show_temp, store_temp, 0x19); |
| 212 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 0x01); |
| 213 | static SENSOR_DEVICE_ATTR_2(temp2_min_alarm, S_IRUGO, |
| 214 | show_bit, NULL, 0x36, 0x02); |
| 215 | static SENSOR_DEVICE_ATTR_2(temp2_max_alarm, S_IRUGO, |
| 216 | show_bit, NULL, 0x35, 0x02); |
| 217 | static SENSOR_DEVICE_ATTR_2(temp2_crit_alarm, S_IRUGO, |
| 218 | show_bit, NULL, 0x37, 0x02); |
| 219 | static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO | S_IWUSR, |
| 220 | show_hyst, store_hyst, 0x19); |
| 221 | |
| 222 | static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO | S_IWUSR, |
| 223 | show_temp, store_temp, 0x16); |
| 224 | static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO | S_IWUSR, |
| 225 | show_temp, store_temp, 0x15); |
| 226 | static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO | S_IWUSR, |
| 227 | show_temp, store_temp, 0x1A); |
| 228 | static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 0x23); |
| 229 | static SENSOR_DEVICE_ATTR_2(temp3_min_alarm, S_IRUGO, |
| 230 | show_bit, NULL, 0x36, 0x04); |
| 231 | static SENSOR_DEVICE_ATTR_2(temp3_max_alarm, S_IRUGO, |
| 232 | show_bit, NULL, 0x35, 0x04); |
| 233 | static SENSOR_DEVICE_ATTR_2(temp3_crit_alarm, S_IRUGO, |
| 234 | show_bit, NULL, 0x37, 0x04); |
| 235 | static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO | S_IWUSR, |
| 236 | show_hyst, store_hyst, 0x1A); |
| 237 | |
Guenter Roeck | 0011ddf | 2013-09-29 13:20:53 -0700 | [diff] [blame] | 238 | static SENSOR_DEVICE_ATTR(temp4_min, S_IRUGO | S_IWUSR, |
| 239 | show_temp, store_temp, 0x2D); |
| 240 | static SENSOR_DEVICE_ATTR(temp4_max, S_IRUGO | S_IWUSR, |
| 241 | show_temp, store_temp, 0x2C); |
| 242 | static SENSOR_DEVICE_ATTR(temp4_crit, S_IRUGO | S_IWUSR, |
| 243 | show_temp, store_temp, 0x30); |
| 244 | static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 0x2A); |
| 245 | static SENSOR_DEVICE_ATTR_2(temp4_min_alarm, S_IRUGO, |
| 246 | show_bit, NULL, 0x36, 0x08); |
| 247 | static SENSOR_DEVICE_ATTR_2(temp4_max_alarm, S_IRUGO, |
| 248 | show_bit, NULL, 0x35, 0x08); |
| 249 | static SENSOR_DEVICE_ATTR_2(temp4_crit_alarm, S_IRUGO, |
| 250 | show_bit, NULL, 0x37, 0x08); |
| 251 | static SENSOR_DEVICE_ATTR(temp4_crit_hyst, S_IRUGO | S_IWUSR, |
| 252 | show_hyst, store_hyst, 0x30); |
| 253 | |
Alan Cox | 960f12f | 2010-08-14 21:08:49 +0200 | [diff] [blame] | 254 | static SENSOR_DEVICE_ATTR_2(power_state, S_IRUGO | S_IWUSR, |
| 255 | show_bit, store_bit, 0x03, 0x40); |
| 256 | |
Josef Gajdusek | be7f5c4 | 2014-05-12 14:34:09 +0200 | [diff] [blame^] | 257 | static struct attribute *emc1402_attrs[] = { |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 258 | &sensor_dev_attr_temp1_min.dev_attr.attr, |
| 259 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 260 | &sensor_dev_attr_temp1_crit.dev_attr.attr, |
| 261 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 262 | &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr, |
Josef Gajdusek | be7f5c4 | 2014-05-12 14:34:09 +0200 | [diff] [blame^] | 263 | |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 264 | &sensor_dev_attr_temp2_min.dev_attr.attr, |
| 265 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
| 266 | &sensor_dev_attr_temp2_crit.dev_attr.attr, |
| 267 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
Josef Gajdusek | be7f5c4 | 2014-05-12 14:34:09 +0200 | [diff] [blame^] | 268 | &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr, |
| 269 | |
| 270 | &sensor_dev_attr_power_state.dev_attr.attr, |
| 271 | NULL |
| 272 | }; |
| 273 | |
| 274 | static const struct attribute_group emc1402_group = { |
| 275 | .attrs = emc1402_attrs, |
| 276 | }; |
| 277 | |
| 278 | static struct attribute *emc1403_attrs[] = { |
| 279 | &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, |
| 280 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, |
| 281 | &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, |
| 282 | |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 283 | &sensor_dev_attr_temp2_min_alarm.dev_attr.attr, |
| 284 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, |
| 285 | &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, |
Josef Gajdusek | be7f5c4 | 2014-05-12 14:34:09 +0200 | [diff] [blame^] | 286 | |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 287 | &sensor_dev_attr_temp3_min.dev_attr.attr, |
| 288 | &sensor_dev_attr_temp3_max.dev_attr.attr, |
| 289 | &sensor_dev_attr_temp3_crit.dev_attr.attr, |
| 290 | &sensor_dev_attr_temp3_input.dev_attr.attr, |
| 291 | &sensor_dev_attr_temp3_min_alarm.dev_attr.attr, |
| 292 | &sensor_dev_attr_temp3_max_alarm.dev_attr.attr, |
| 293 | &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr, |
| 294 | &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr, |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 295 | NULL |
| 296 | }; |
Guenter Roeck | 0011ddf | 2013-09-29 13:20:53 -0700 | [diff] [blame] | 297 | |
| 298 | static const struct attribute_group emc1403_group = { |
| 299 | .attrs = emc1403_attrs, |
| 300 | }; |
| 301 | |
| 302 | static struct attribute *emc1404_attrs[] = { |
| 303 | &sensor_dev_attr_temp4_min.dev_attr.attr, |
| 304 | &sensor_dev_attr_temp4_max.dev_attr.attr, |
| 305 | &sensor_dev_attr_temp4_crit.dev_attr.attr, |
| 306 | &sensor_dev_attr_temp4_input.dev_attr.attr, |
| 307 | &sensor_dev_attr_temp4_min_alarm.dev_attr.attr, |
| 308 | &sensor_dev_attr_temp4_max_alarm.dev_attr.attr, |
| 309 | &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr, |
| 310 | &sensor_dev_attr_temp4_crit_hyst.dev_attr.attr, |
| 311 | NULL |
| 312 | }; |
| 313 | |
| 314 | static const struct attribute_group emc1404_group = { |
| 315 | .attrs = emc1404_attrs, |
| 316 | }; |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 317 | |
| 318 | static int emc1403_detect(struct i2c_client *client, |
| 319 | struct i2c_board_info *info) |
| 320 | { |
| 321 | int id; |
Jekyll Lai | 7a1b76f | 2011-01-12 21:55:12 +0100 | [diff] [blame] | 322 | /* Check if thermal chip is SMSC and EMC1403 or EMC1423 */ |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 323 | |
| 324 | id = i2c_smbus_read_byte_data(client, THERMAL_SMSC_ID_REG); |
| 325 | if (id != 0x5d) |
| 326 | return -ENODEV; |
| 327 | |
Jekyll Lai | 7a1b76f | 2011-01-12 21:55:12 +0100 | [diff] [blame] | 328 | id = i2c_smbus_read_byte_data(client, THERMAL_PID_REG); |
| 329 | switch (id) { |
Josef Gajdusek | be7f5c4 | 2014-05-12 14:34:09 +0200 | [diff] [blame^] | 330 | case 0x20: |
| 331 | strlcpy(info->type, "emc1402", I2C_NAME_SIZE); |
| 332 | break; |
Jekyll Lai | 7a1b76f | 2011-01-12 21:55:12 +0100 | [diff] [blame] | 333 | case 0x21: |
| 334 | strlcpy(info->type, "emc1403", I2C_NAME_SIZE); |
| 335 | break; |
Josef Gajdusek | be7f5c4 | 2014-05-12 14:34:09 +0200 | [diff] [blame^] | 336 | case 0x22: |
| 337 | strlcpy(info->type, "emc1422", I2C_NAME_SIZE); |
| 338 | break; |
Jekyll Lai | 7a1b76f | 2011-01-12 21:55:12 +0100 | [diff] [blame] | 339 | case 0x23: |
| 340 | strlcpy(info->type, "emc1423", I2C_NAME_SIZE); |
| 341 | break; |
Guenter Roeck | 0011ddf | 2013-09-29 13:20:53 -0700 | [diff] [blame] | 342 | case 0x25: |
| 343 | strlcpy(info->type, "emc1404", I2C_NAME_SIZE); |
| 344 | break; |
| 345 | case 0x27: |
| 346 | strlcpy(info->type, "emc1424", I2C_NAME_SIZE); |
| 347 | break; |
Jekyll Lai | 7a1b76f | 2011-01-12 21:55:12 +0100 | [diff] [blame] | 348 | default: |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 349 | return -ENODEV; |
Jekyll Lai | 7a1b76f | 2011-01-12 21:55:12 +0100 | [diff] [blame] | 350 | } |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 351 | |
| 352 | id = i2c_smbus_read_byte_data(client, THERMAL_REVISION_REG); |
Josef Gajdusek | 3a18e13 | 2014-05-12 13:48:26 +0200 | [diff] [blame] | 353 | if (id < 0x01 || id > 0x04) |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 354 | return -ENODEV; |
| 355 | |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | static int emc1403_probe(struct i2c_client *client, |
| 360 | const struct i2c_device_id *id) |
| 361 | { |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 362 | struct thermal_data *data; |
Guenter Roeck | 454aee1 | 2013-09-29 11:49:53 -0700 | [diff] [blame] | 363 | struct device *hwmon_dev; |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 364 | |
Guenter Roeck | 7b52eef | 2012-06-02 09:58:04 -0700 | [diff] [blame] | 365 | data = devm_kzalloc(&client->dev, sizeof(struct thermal_data), |
| 366 | GFP_KERNEL); |
| 367 | if (data == NULL) |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 368 | return -ENOMEM; |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 369 | |
Guenter Roeck | 454aee1 | 2013-09-29 11:49:53 -0700 | [diff] [blame] | 370 | data->client = client; |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 371 | mutex_init(&data->mutex); |
| 372 | data->hyst_valid = jiffies - 1; /* Expired */ |
| 373 | |
Josef Gajdusek | be7f5c4 | 2014-05-12 14:34:09 +0200 | [diff] [blame^] | 374 | switch (id->driver_data) { |
| 375 | case emc1404: |
| 376 | data->groups[2] = &emc1404_group; |
| 377 | case emc1403: |
| 378 | data->groups[1] = &emc1403_group; |
| 379 | case emc1402: |
| 380 | data->groups[0] = &emc1402_group; |
| 381 | } |
Guenter Roeck | 0011ddf | 2013-09-29 13:20:53 -0700 | [diff] [blame] | 382 | |
Jean Delvare | 8759f90 | 2014-05-12 11:44:51 +0200 | [diff] [blame] | 383 | hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev, |
| 384 | client->name, data, |
| 385 | data->groups); |
Guenter Roeck | 454aee1 | 2013-09-29 11:49:53 -0700 | [diff] [blame] | 386 | if (IS_ERR(hwmon_dev)) |
| 387 | return PTR_ERR(hwmon_dev); |
| 388 | |
Guenter Roeck | 0011ddf | 2013-09-29 13:20:53 -0700 | [diff] [blame] | 389 | dev_info(&client->dev, "%s Thermal chip found\n", id->name); |
Guenter Roeck | 7b52eef | 2012-06-02 09:58:04 -0700 | [diff] [blame] | 390 | return 0; |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | static const unsigned short emc1403_address_list[] = { |
Josef Gajdusek | be7f5c4 | 2014-05-12 14:34:09 +0200 | [diff] [blame^] | 394 | 0x18, 0x1c, 0x29, 0x4c, 0x4d, 0x5c, I2C_CLIENT_END |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 395 | }; |
| 396 | |
Josef Gajdusek | be7f5c4 | 2014-05-12 14:34:09 +0200 | [diff] [blame^] | 397 | /* Last digit of chip name indicates number of channels */ |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 398 | static const struct i2c_device_id emc1403_idtable[] = { |
Josef Gajdusek | be7f5c4 | 2014-05-12 14:34:09 +0200 | [diff] [blame^] | 399 | { "emc1402", emc1402 }, |
| 400 | { "emc1403", emc1403 }, |
| 401 | { "emc1404", emc1404 }, |
| 402 | { "emc1422", emc1402 }, |
| 403 | { "emc1423", emc1403 }, |
| 404 | { "emc1424", emc1404 }, |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 405 | { } |
| 406 | }; |
| 407 | MODULE_DEVICE_TABLE(i2c, emc1403_idtable); |
| 408 | |
| 409 | static struct i2c_driver sensor_emc1403 = { |
| 410 | .class = I2C_CLASS_HWMON, |
| 411 | .driver = { |
| 412 | .name = "emc1403", |
| 413 | }, |
| 414 | .detect = emc1403_detect, |
| 415 | .probe = emc1403_probe, |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 416 | .id_table = emc1403_idtable, |
| 417 | .address_list = emc1403_address_list, |
| 418 | }; |
| 419 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 420 | module_i2c_driver(sensor_emc1403); |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 421 | |
| 422 | MODULE_AUTHOR("Kalhan Trisal <kalhan.trisal@intel.com"); |
| 423 | MODULE_DESCRIPTION("emc1403 Thermal Driver"); |
| 424 | MODULE_LICENSE("GPL v2"); |