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 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/i2c.h> |
| 27 | #include <linux/hwmon.h> |
| 28 | #include <linux/hwmon-sysfs.h> |
| 29 | #include <linux/err.h> |
| 30 | #include <linux/sysfs.h> |
| 31 | #include <linux/mutex.h> |
Guenter Roeck | 4cab259 | 2014-05-12 10:44:48 -0700 | [diff] [blame] | 32 | #include <linux/regmap.h> |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 33 | |
| 34 | #define THERMAL_PID_REG 0xfd |
| 35 | #define THERMAL_SMSC_ID_REG 0xfe |
| 36 | #define THERMAL_REVISION_REG 0xff |
| 37 | |
Josef Gajdusek | be7f5c4 | 2014-05-12 14:34:09 +0200 | [diff] [blame] | 38 | enum emc1403_chip { emc1402, emc1403, emc1404 }; |
| 39 | |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 40 | struct thermal_data { |
Guenter Roeck | 4cab259 | 2014-05-12 10:44:48 -0700 | [diff] [blame] | 41 | struct regmap *regmap; |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 42 | struct mutex mutex; |
Guenter Roeck | 4cab259 | 2014-05-12 10:44:48 -0700 | [diff] [blame] | 43 | const struct attribute_group *groups[4]; |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | static ssize_t show_temp(struct device *dev, |
| 47 | struct device_attribute *attr, char *buf) |
| 48 | { |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 49 | struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); |
Guenter Roeck | 454aee1 | 2013-09-29 11:49:53 -0700 | [diff] [blame] | 50 | struct thermal_data *data = dev_get_drvdata(dev); |
Guenter Roeck | 4cab259 | 2014-05-12 10:44:48 -0700 | [diff] [blame] | 51 | unsigned int val; |
Guenter Roeck | 454aee1 | 2013-09-29 11:49:53 -0700 | [diff] [blame] | 52 | int retval; |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 53 | |
Guenter Roeck | 4cab259 | 2014-05-12 10:44:48 -0700 | [diff] [blame] | 54 | retval = regmap_read(data->regmap, sda->index, &val); |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 55 | if (retval < 0) |
| 56 | return retval; |
Guenter Roeck | 4cab259 | 2014-05-12 10:44:48 -0700 | [diff] [blame] | 57 | return sprintf(buf, "%d000\n", val); |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static ssize_t show_bit(struct device *dev, |
| 61 | struct device_attribute *attr, char *buf) |
| 62 | { |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 63 | struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr); |
Guenter Roeck | 454aee1 | 2013-09-29 11:49:53 -0700 | [diff] [blame] | 64 | struct thermal_data *data = dev_get_drvdata(dev); |
Guenter Roeck | 4cab259 | 2014-05-12 10:44:48 -0700 | [diff] [blame] | 65 | unsigned int val; |
Guenter Roeck | 454aee1 | 2013-09-29 11:49:53 -0700 | [diff] [blame] | 66 | int retval; |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 67 | |
Guenter Roeck | 4cab259 | 2014-05-12 10:44:48 -0700 | [diff] [blame] | 68 | retval = regmap_read(data->regmap, sda->nr, &val); |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 69 | if (retval < 0) |
| 70 | return retval; |
Guenter Roeck | 4cab259 | 2014-05-12 10:44:48 -0700 | [diff] [blame] | 71 | return sprintf(buf, "%d\n", !!(val & sda->index)); |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static ssize_t store_temp(struct device *dev, |
| 75 | struct device_attribute *attr, const char *buf, size_t count) |
| 76 | { |
| 77 | struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); |
Guenter Roeck | 454aee1 | 2013-09-29 11:49:53 -0700 | [diff] [blame] | 78 | struct thermal_data *data = dev_get_drvdata(dev); |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 79 | unsigned long val; |
| 80 | int retval; |
| 81 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 82 | if (kstrtoul(buf, 10, &val)) |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 83 | return -EINVAL; |
Guenter Roeck | 4cab259 | 2014-05-12 10:44:48 -0700 | [diff] [blame] | 84 | retval = regmap_write(data->regmap, sda->index, |
| 85 | DIV_ROUND_CLOSEST(val, 1000)); |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 86 | if (retval < 0) |
| 87 | return retval; |
| 88 | return count; |
| 89 | } |
| 90 | |
Alan Cox | 960f12f | 2010-08-14 21:08:49 +0200 | [diff] [blame] | 91 | static ssize_t store_bit(struct device *dev, |
| 92 | struct device_attribute *attr, const char *buf, size_t count) |
| 93 | { |
Alan Cox | 960f12f | 2010-08-14 21:08:49 +0200 | [diff] [blame] | 94 | struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr); |
Guenter Roeck | 454aee1 | 2013-09-29 11:49:53 -0700 | [diff] [blame] | 95 | struct thermal_data *data = dev_get_drvdata(dev); |
Alan Cox | 960f12f | 2010-08-14 21:08:49 +0200 | [diff] [blame] | 96 | unsigned long val; |
| 97 | int retval; |
| 98 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 99 | if (kstrtoul(buf, 10, &val)) |
Alan Cox | 960f12f | 2010-08-14 21:08:49 +0200 | [diff] [blame] | 100 | return -EINVAL; |
| 101 | |
Guenter Roeck | 4cab259 | 2014-05-12 10:44:48 -0700 | [diff] [blame] | 102 | retval = regmap_update_bits(data->regmap, sda->nr, sda->index, |
| 103 | val ? sda->index : 0); |
Alan Cox | 960f12f | 2010-08-14 21:08:49 +0200 | [diff] [blame] | 104 | if (retval < 0) |
Guenter Roeck | 4cab259 | 2014-05-12 10:44:48 -0700 | [diff] [blame] | 105 | return retval; |
| 106 | return count; |
Alan Cox | 960f12f | 2010-08-14 21:08:49 +0200 | [diff] [blame] | 107 | } |
| 108 | |
Guenter Roeck | 54392ce | 2014-05-12 11:35:06 -0700 | [diff] [blame] | 109 | static ssize_t show_hyst_common(struct device *dev, |
| 110 | struct device_attribute *attr, char *buf, |
| 111 | bool is_min) |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 112 | { |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 113 | struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); |
Guenter Roeck | 454aee1 | 2013-09-29 11:49:53 -0700 | [diff] [blame] | 114 | struct thermal_data *data = dev_get_drvdata(dev); |
Guenter Roeck | 4cab259 | 2014-05-12 10:44:48 -0700 | [diff] [blame] | 115 | struct regmap *regmap = data->regmap; |
| 116 | unsigned int limit; |
| 117 | unsigned int hyst; |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 118 | int retval; |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 119 | |
Guenter Roeck | 4cab259 | 2014-05-12 10:44:48 -0700 | [diff] [blame] | 120 | retval = regmap_read(regmap, sda->index, &limit); |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 121 | if (retval < 0) |
| 122 | return retval; |
| 123 | |
Guenter Roeck | 4cab259 | 2014-05-12 10:44:48 -0700 | [diff] [blame] | 124 | retval = regmap_read(regmap, 0x21, &hyst); |
| 125 | if (retval < 0) |
| 126 | return retval; |
| 127 | |
Guenter Roeck | 54392ce | 2014-05-12 11:35:06 -0700 | [diff] [blame] | 128 | return sprintf(buf, "%d000\n", is_min ? limit + hyst : limit - hyst); |
| 129 | } |
| 130 | |
| 131 | static ssize_t show_hyst(struct device *dev, |
| 132 | struct device_attribute *attr, char *buf) |
| 133 | { |
| 134 | return show_hyst_common(dev, attr, buf, false); |
| 135 | } |
| 136 | |
| 137 | static ssize_t show_min_hyst(struct device *dev, |
| 138 | struct device_attribute *attr, char *buf) |
| 139 | { |
| 140 | return show_hyst_common(dev, attr, buf, true); |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static ssize_t store_hyst(struct device *dev, |
| 144 | struct device_attribute *attr, const char *buf, size_t count) |
| 145 | { |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 146 | struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); |
Guenter Roeck | 454aee1 | 2013-09-29 11:49:53 -0700 | [diff] [blame] | 147 | struct thermal_data *data = dev_get_drvdata(dev); |
Guenter Roeck | 4cab259 | 2014-05-12 10:44:48 -0700 | [diff] [blame] | 148 | struct regmap *regmap = data->regmap; |
| 149 | unsigned int limit; |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 150 | int retval; |
| 151 | int hyst; |
| 152 | unsigned long val; |
| 153 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 154 | if (kstrtoul(buf, 10, &val)) |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 155 | return -EINVAL; |
| 156 | |
| 157 | mutex_lock(&data->mutex); |
Guenter Roeck | 4cab259 | 2014-05-12 10:44:48 -0700 | [diff] [blame] | 158 | retval = regmap_read(regmap, sda->index, &limit); |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 159 | if (retval < 0) |
| 160 | goto fail; |
| 161 | |
Guenter Roeck | 4cab259 | 2014-05-12 10:44:48 -0700 | [diff] [blame] | 162 | hyst = limit * 1000 - val; |
Guenter Roeck | ceeaa70 | 2014-05-12 11:20:24 -0700 | [diff] [blame] | 163 | hyst = clamp_val(DIV_ROUND_CLOSEST(hyst, 1000), 0, 255); |
Guenter Roeck | 4cab259 | 2014-05-12 10:44:48 -0700 | [diff] [blame] | 164 | retval = regmap_write(regmap, 0x21, hyst); |
| 165 | if (retval == 0) |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 166 | retval = count; |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 167 | fail: |
| 168 | mutex_unlock(&data->mutex); |
| 169 | return retval; |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | * Sensors. We pass the actual i2c register to the methods. |
| 174 | */ |
| 175 | |
| 176 | static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO | S_IWUSR, |
| 177 | show_temp, store_temp, 0x06); |
| 178 | static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, |
| 179 | show_temp, store_temp, 0x05); |
| 180 | static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR, |
| 181 | show_temp, store_temp, 0x20); |
| 182 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0x00); |
| 183 | static SENSOR_DEVICE_ATTR_2(temp1_min_alarm, S_IRUGO, |
| 184 | show_bit, NULL, 0x36, 0x01); |
| 185 | static SENSOR_DEVICE_ATTR_2(temp1_max_alarm, S_IRUGO, |
| 186 | show_bit, NULL, 0x35, 0x01); |
| 187 | static SENSOR_DEVICE_ATTR_2(temp1_crit_alarm, S_IRUGO, |
| 188 | show_bit, NULL, 0x37, 0x01); |
Guenter Roeck | 54392ce | 2014-05-12 11:35:06 -0700 | [diff] [blame] | 189 | static SENSOR_DEVICE_ATTR(temp1_min_hyst, S_IRUGO, show_min_hyst, NULL, 0x06); |
Guenter Roeck | a9a7400 | 2014-05-12 11:25:47 -0700 | [diff] [blame] | 190 | static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO, show_hyst, NULL, 0x05); |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 191 | static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO | S_IWUSR, |
| 192 | show_hyst, store_hyst, 0x20); |
| 193 | |
| 194 | static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO | S_IWUSR, |
| 195 | show_temp, store_temp, 0x08); |
| 196 | static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR, |
| 197 | show_temp, store_temp, 0x07); |
| 198 | static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO | S_IWUSR, |
| 199 | show_temp, store_temp, 0x19); |
| 200 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 0x01); |
Guenter Roeck | d8850c1 | 2014-05-12 10:48:02 -0700 | [diff] [blame] | 201 | static SENSOR_DEVICE_ATTR_2(temp2_fault, S_IRUGO, show_bit, NULL, 0x1b, 0x02); |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 202 | static SENSOR_DEVICE_ATTR_2(temp2_min_alarm, S_IRUGO, |
| 203 | show_bit, NULL, 0x36, 0x02); |
| 204 | static SENSOR_DEVICE_ATTR_2(temp2_max_alarm, S_IRUGO, |
| 205 | show_bit, NULL, 0x35, 0x02); |
| 206 | static SENSOR_DEVICE_ATTR_2(temp2_crit_alarm, S_IRUGO, |
| 207 | show_bit, NULL, 0x37, 0x02); |
Guenter Roeck | 54392ce | 2014-05-12 11:35:06 -0700 | [diff] [blame] | 208 | static SENSOR_DEVICE_ATTR(temp2_min_hyst, S_IRUGO, show_min_hyst, NULL, 0x08); |
Guenter Roeck | a9a7400 | 2014-05-12 11:25:47 -0700 | [diff] [blame] | 209 | static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO, show_hyst, NULL, 0x07); |
Guenter Roeck | 84899d3 | 2014-05-12 11:10:56 -0700 | [diff] [blame] | 210 | static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_hyst, NULL, 0x19); |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 211 | |
| 212 | static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO | S_IWUSR, |
| 213 | show_temp, store_temp, 0x16); |
| 214 | static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO | S_IWUSR, |
| 215 | show_temp, store_temp, 0x15); |
| 216 | static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO | S_IWUSR, |
| 217 | show_temp, store_temp, 0x1A); |
| 218 | static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 0x23); |
Guenter Roeck | d8850c1 | 2014-05-12 10:48:02 -0700 | [diff] [blame] | 219 | static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_bit, NULL, 0x1b, 0x04); |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 220 | static SENSOR_DEVICE_ATTR_2(temp3_min_alarm, S_IRUGO, |
| 221 | show_bit, NULL, 0x36, 0x04); |
| 222 | static SENSOR_DEVICE_ATTR_2(temp3_max_alarm, S_IRUGO, |
| 223 | show_bit, NULL, 0x35, 0x04); |
| 224 | static SENSOR_DEVICE_ATTR_2(temp3_crit_alarm, S_IRUGO, |
| 225 | show_bit, NULL, 0x37, 0x04); |
Guenter Roeck | 54392ce | 2014-05-12 11:35:06 -0700 | [diff] [blame] | 226 | static SENSOR_DEVICE_ATTR(temp3_min_hyst, S_IRUGO, show_min_hyst, NULL, 0x16); |
Guenter Roeck | a9a7400 | 2014-05-12 11:25:47 -0700 | [diff] [blame] | 227 | static SENSOR_DEVICE_ATTR(temp3_max_hyst, S_IRUGO, show_hyst, NULL, 0x15); |
Guenter Roeck | 84899d3 | 2014-05-12 11:10:56 -0700 | [diff] [blame] | 228 | static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, show_hyst, NULL, 0x1A); |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 229 | |
Guenter Roeck | 0011ddf | 2013-09-29 13:20:53 -0700 | [diff] [blame] | 230 | static SENSOR_DEVICE_ATTR(temp4_min, S_IRUGO | S_IWUSR, |
| 231 | show_temp, store_temp, 0x2D); |
| 232 | static SENSOR_DEVICE_ATTR(temp4_max, S_IRUGO | S_IWUSR, |
| 233 | show_temp, store_temp, 0x2C); |
| 234 | static SENSOR_DEVICE_ATTR(temp4_crit, S_IRUGO | S_IWUSR, |
| 235 | show_temp, store_temp, 0x30); |
| 236 | static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 0x2A); |
Guenter Roeck | d8850c1 | 2014-05-12 10:48:02 -0700 | [diff] [blame] | 237 | static SENSOR_DEVICE_ATTR_2(temp4_fault, S_IRUGO, show_bit, NULL, 0x1b, 0x08); |
Guenter Roeck | 0011ddf | 2013-09-29 13:20:53 -0700 | [diff] [blame] | 238 | static SENSOR_DEVICE_ATTR_2(temp4_min_alarm, S_IRUGO, |
| 239 | show_bit, NULL, 0x36, 0x08); |
| 240 | static SENSOR_DEVICE_ATTR_2(temp4_max_alarm, S_IRUGO, |
| 241 | show_bit, NULL, 0x35, 0x08); |
| 242 | static SENSOR_DEVICE_ATTR_2(temp4_crit_alarm, S_IRUGO, |
| 243 | show_bit, NULL, 0x37, 0x08); |
Guenter Roeck | 54392ce | 2014-05-12 11:35:06 -0700 | [diff] [blame] | 244 | static SENSOR_DEVICE_ATTR(temp4_min_hyst, S_IRUGO, show_min_hyst, NULL, 0x2D); |
Guenter Roeck | a9a7400 | 2014-05-12 11:25:47 -0700 | [diff] [blame] | 245 | static SENSOR_DEVICE_ATTR(temp4_max_hyst, S_IRUGO, show_hyst, NULL, 0x2C); |
Guenter Roeck | 84899d3 | 2014-05-12 11:10:56 -0700 | [diff] [blame] | 246 | static SENSOR_DEVICE_ATTR(temp4_crit_hyst, S_IRUGO, show_hyst, NULL, 0x30); |
Guenter Roeck | 0011ddf | 2013-09-29 13:20:53 -0700 | [diff] [blame] | 247 | |
Alan Cox | 960f12f | 2010-08-14 21:08:49 +0200 | [diff] [blame] | 248 | static SENSOR_DEVICE_ATTR_2(power_state, S_IRUGO | S_IWUSR, |
| 249 | show_bit, store_bit, 0x03, 0x40); |
| 250 | |
Josef Gajdusek | be7f5c4 | 2014-05-12 14:34:09 +0200 | [diff] [blame] | 251 | static struct attribute *emc1402_attrs[] = { |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 252 | &sensor_dev_attr_temp1_min.dev_attr.attr, |
| 253 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 254 | &sensor_dev_attr_temp1_crit.dev_attr.attr, |
| 255 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
Guenter Roeck | 54392ce | 2014-05-12 11:35:06 -0700 | [diff] [blame] | 256 | &sensor_dev_attr_temp1_min_hyst.dev_attr.attr, |
Guenter Roeck | a9a7400 | 2014-05-12 11:25:47 -0700 | [diff] [blame] | 257 | &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 258 | &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr, |
Josef Gajdusek | be7f5c4 | 2014-05-12 14:34:09 +0200 | [diff] [blame] | 259 | |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 260 | &sensor_dev_attr_temp2_min.dev_attr.attr, |
| 261 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
| 262 | &sensor_dev_attr_temp2_crit.dev_attr.attr, |
| 263 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
Guenter Roeck | 54392ce | 2014-05-12 11:35:06 -0700 | [diff] [blame] | 264 | &sensor_dev_attr_temp2_min_hyst.dev_attr.attr, |
Guenter Roeck | a9a7400 | 2014-05-12 11:25:47 -0700 | [diff] [blame] | 265 | &sensor_dev_attr_temp2_max_hyst.dev_attr.attr, |
Josef Gajdusek | be7f5c4 | 2014-05-12 14:34:09 +0200 | [diff] [blame] | 266 | &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr, |
| 267 | |
| 268 | &sensor_dev_attr_power_state.dev_attr.attr, |
| 269 | NULL |
| 270 | }; |
| 271 | |
| 272 | static const struct attribute_group emc1402_group = { |
| 273 | .attrs = emc1402_attrs, |
| 274 | }; |
| 275 | |
| 276 | static struct attribute *emc1403_attrs[] = { |
| 277 | &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, |
| 278 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, |
| 279 | &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, |
| 280 | |
Guenter Roeck | d8850c1 | 2014-05-12 10:48:02 -0700 | [diff] [blame] | 281 | &sensor_dev_attr_temp2_fault.dev_attr.attr, |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 282 | &sensor_dev_attr_temp2_min_alarm.dev_attr.attr, |
| 283 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, |
| 284 | &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, |
Josef Gajdusek | be7f5c4 | 2014-05-12 14:34:09 +0200 | [diff] [blame] | 285 | |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 286 | &sensor_dev_attr_temp3_min.dev_attr.attr, |
| 287 | &sensor_dev_attr_temp3_max.dev_attr.attr, |
| 288 | &sensor_dev_attr_temp3_crit.dev_attr.attr, |
| 289 | &sensor_dev_attr_temp3_input.dev_attr.attr, |
Guenter Roeck | d8850c1 | 2014-05-12 10:48:02 -0700 | [diff] [blame] | 290 | &sensor_dev_attr_temp3_fault.dev_attr.attr, |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 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, |
Guenter Roeck | 54392ce | 2014-05-12 11:35:06 -0700 | [diff] [blame] | 294 | &sensor_dev_attr_temp3_min_hyst.dev_attr.attr, |
Guenter Roeck | a9a7400 | 2014-05-12 11:25:47 -0700 | [diff] [blame] | 295 | &sensor_dev_attr_temp3_max_hyst.dev_attr.attr, |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 296 | &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr, |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 297 | NULL |
| 298 | }; |
Guenter Roeck | 0011ddf | 2013-09-29 13:20:53 -0700 | [diff] [blame] | 299 | |
| 300 | static const struct attribute_group emc1403_group = { |
| 301 | .attrs = emc1403_attrs, |
| 302 | }; |
| 303 | |
| 304 | static struct attribute *emc1404_attrs[] = { |
| 305 | &sensor_dev_attr_temp4_min.dev_attr.attr, |
| 306 | &sensor_dev_attr_temp4_max.dev_attr.attr, |
| 307 | &sensor_dev_attr_temp4_crit.dev_attr.attr, |
| 308 | &sensor_dev_attr_temp4_input.dev_attr.attr, |
Guenter Roeck | d8850c1 | 2014-05-12 10:48:02 -0700 | [diff] [blame] | 309 | &sensor_dev_attr_temp4_fault.dev_attr.attr, |
Guenter Roeck | 0011ddf | 2013-09-29 13:20:53 -0700 | [diff] [blame] | 310 | &sensor_dev_attr_temp4_min_alarm.dev_attr.attr, |
| 311 | &sensor_dev_attr_temp4_max_alarm.dev_attr.attr, |
| 312 | &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr, |
Guenter Roeck | 54392ce | 2014-05-12 11:35:06 -0700 | [diff] [blame] | 313 | &sensor_dev_attr_temp4_min_hyst.dev_attr.attr, |
Guenter Roeck | a9a7400 | 2014-05-12 11:25:47 -0700 | [diff] [blame] | 314 | &sensor_dev_attr_temp4_max_hyst.dev_attr.attr, |
Guenter Roeck | 0011ddf | 2013-09-29 13:20:53 -0700 | [diff] [blame] | 315 | &sensor_dev_attr_temp4_crit_hyst.dev_attr.attr, |
| 316 | NULL |
| 317 | }; |
| 318 | |
| 319 | static const struct attribute_group emc1404_group = { |
| 320 | .attrs = emc1404_attrs, |
| 321 | }; |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 322 | |
Guenter Roeck | 03f49f6 | 2014-05-12 11:03:47 -0700 | [diff] [blame] | 323 | /* |
| 324 | * EMC14x2 uses a different register and different bits to report alarm and |
| 325 | * fault status. For simplicity, provide a separate attribute group for this |
| 326 | * chip series. |
| 327 | * Since we can not re-use the same attribute names, create a separate attribute |
| 328 | * array. |
| 329 | */ |
| 330 | static struct sensor_device_attribute_2 emc1402_alarms[] = { |
| 331 | SENSOR_ATTR_2(temp1_min_alarm, S_IRUGO, show_bit, NULL, 0x02, 0x20), |
| 332 | SENSOR_ATTR_2(temp1_max_alarm, S_IRUGO, show_bit, NULL, 0x02, 0x40), |
| 333 | SENSOR_ATTR_2(temp1_crit_alarm, S_IRUGO, show_bit, NULL, 0x02, 0x01), |
| 334 | |
| 335 | SENSOR_ATTR_2(temp2_fault, S_IRUGO, show_bit, NULL, 0x02, 0x04), |
| 336 | SENSOR_ATTR_2(temp2_min_alarm, S_IRUGO, show_bit, NULL, 0x02, 0x08), |
| 337 | SENSOR_ATTR_2(temp2_max_alarm, S_IRUGO, show_bit, NULL, 0x02, 0x10), |
| 338 | SENSOR_ATTR_2(temp2_crit_alarm, S_IRUGO, show_bit, NULL, 0x02, 0x02), |
| 339 | }; |
| 340 | |
| 341 | static struct attribute *emc1402_alarm_attrs[] = { |
| 342 | &emc1402_alarms[0].dev_attr.attr, |
| 343 | &emc1402_alarms[1].dev_attr.attr, |
| 344 | &emc1402_alarms[2].dev_attr.attr, |
| 345 | &emc1402_alarms[3].dev_attr.attr, |
| 346 | &emc1402_alarms[4].dev_attr.attr, |
| 347 | &emc1402_alarms[5].dev_attr.attr, |
| 348 | &emc1402_alarms[6].dev_attr.attr, |
| 349 | NULL, |
| 350 | }; |
| 351 | |
| 352 | static const struct attribute_group emc1402_alarm_group = { |
| 353 | .attrs = emc1402_alarm_attrs, |
| 354 | }; |
| 355 | |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 356 | static int emc1403_detect(struct i2c_client *client, |
| 357 | struct i2c_board_info *info) |
| 358 | { |
| 359 | int id; |
Jekyll Lai | 7a1b76f | 2011-01-12 21:55:12 +0100 | [diff] [blame] | 360 | /* Check if thermal chip is SMSC and EMC1403 or EMC1423 */ |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 361 | |
| 362 | id = i2c_smbus_read_byte_data(client, THERMAL_SMSC_ID_REG); |
| 363 | if (id != 0x5d) |
| 364 | return -ENODEV; |
| 365 | |
Jekyll Lai | 7a1b76f | 2011-01-12 21:55:12 +0100 | [diff] [blame] | 366 | id = i2c_smbus_read_byte_data(client, THERMAL_PID_REG); |
| 367 | switch (id) { |
Josef Gajdusek | be7f5c4 | 2014-05-12 14:34:09 +0200 | [diff] [blame] | 368 | case 0x20: |
| 369 | strlcpy(info->type, "emc1402", I2C_NAME_SIZE); |
| 370 | break; |
Jekyll Lai | 7a1b76f | 2011-01-12 21:55:12 +0100 | [diff] [blame] | 371 | case 0x21: |
| 372 | strlcpy(info->type, "emc1403", I2C_NAME_SIZE); |
| 373 | break; |
Josef Gajdusek | be7f5c4 | 2014-05-12 14:34:09 +0200 | [diff] [blame] | 374 | case 0x22: |
| 375 | strlcpy(info->type, "emc1422", I2C_NAME_SIZE); |
| 376 | break; |
Jekyll Lai | 7a1b76f | 2011-01-12 21:55:12 +0100 | [diff] [blame] | 377 | case 0x23: |
| 378 | strlcpy(info->type, "emc1423", I2C_NAME_SIZE); |
| 379 | break; |
Guenter Roeck | 0011ddf | 2013-09-29 13:20:53 -0700 | [diff] [blame] | 380 | case 0x25: |
| 381 | strlcpy(info->type, "emc1404", I2C_NAME_SIZE); |
| 382 | break; |
| 383 | case 0x27: |
| 384 | strlcpy(info->type, "emc1424", I2C_NAME_SIZE); |
| 385 | break; |
Jekyll Lai | 7a1b76f | 2011-01-12 21:55:12 +0100 | [diff] [blame] | 386 | default: |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 387 | return -ENODEV; |
Jekyll Lai | 7a1b76f | 2011-01-12 21:55:12 +0100 | [diff] [blame] | 388 | } |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 389 | |
| 390 | id = i2c_smbus_read_byte_data(client, THERMAL_REVISION_REG); |
Josef Gajdusek | 3a18e13 | 2014-05-12 13:48:26 +0200 | [diff] [blame] | 391 | if (id < 0x01 || id > 0x04) |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 392 | return -ENODEV; |
| 393 | |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 394 | return 0; |
| 395 | } |
| 396 | |
Guenter Roeck | 4cab259 | 2014-05-12 10:44:48 -0700 | [diff] [blame] | 397 | static bool emc1403_regmap_is_volatile(struct device *dev, unsigned int reg) |
| 398 | { |
| 399 | switch (reg) { |
| 400 | case 0x00: /* internal diode high byte */ |
| 401 | case 0x01: /* external diode 1 high byte */ |
| 402 | case 0x02: /* status */ |
| 403 | case 0x10: /* external diode 1 low byte */ |
| 404 | case 0x1b: /* external diode fault */ |
| 405 | case 0x23: /* external diode 2 high byte */ |
| 406 | case 0x24: /* external diode 2 low byte */ |
| 407 | case 0x29: /* internal diode low byte */ |
| 408 | case 0x2a: /* externl diode 3 high byte */ |
| 409 | case 0x2b: /* external diode 3 low byte */ |
| 410 | case 0x35: /* high limit status */ |
| 411 | case 0x36: /* low limit status */ |
| 412 | case 0x37: /* therm limit status */ |
| 413 | return true; |
| 414 | default: |
| 415 | return false; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | static struct regmap_config emc1403_regmap_config = { |
| 420 | .reg_bits = 8, |
| 421 | .val_bits = 8, |
| 422 | .cache_type = REGCACHE_RBTREE, |
| 423 | .volatile_reg = emc1403_regmap_is_volatile, |
| 424 | }; |
| 425 | |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 426 | static int emc1403_probe(struct i2c_client *client, |
| 427 | const struct i2c_device_id *id) |
| 428 | { |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 429 | struct thermal_data *data; |
Guenter Roeck | 454aee1 | 2013-09-29 11:49:53 -0700 | [diff] [blame] | 430 | struct device *hwmon_dev; |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 431 | |
Guenter Roeck | 7b52eef | 2012-06-02 09:58:04 -0700 | [diff] [blame] | 432 | data = devm_kzalloc(&client->dev, sizeof(struct thermal_data), |
| 433 | GFP_KERNEL); |
| 434 | if (data == NULL) |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 435 | return -ENOMEM; |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 436 | |
Guenter Roeck | 4cab259 | 2014-05-12 10:44:48 -0700 | [diff] [blame] | 437 | data->regmap = devm_regmap_init_i2c(client, &emc1403_regmap_config); |
| 438 | if (IS_ERR(data->regmap)) |
| 439 | return PTR_ERR(data->regmap); |
| 440 | |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 441 | mutex_init(&data->mutex); |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 442 | |
Josef Gajdusek | be7f5c4 | 2014-05-12 14:34:09 +0200 | [diff] [blame] | 443 | switch (id->driver_data) { |
| 444 | case emc1404: |
| 445 | data->groups[2] = &emc1404_group; |
| 446 | case emc1403: |
| 447 | data->groups[1] = &emc1403_group; |
| 448 | case emc1402: |
| 449 | data->groups[0] = &emc1402_group; |
| 450 | } |
Guenter Roeck | 0011ddf | 2013-09-29 13:20:53 -0700 | [diff] [blame] | 451 | |
Guenter Roeck | 03f49f6 | 2014-05-12 11:03:47 -0700 | [diff] [blame] | 452 | if (id->driver_data == emc1402) |
| 453 | data->groups[1] = &emc1402_alarm_group; |
| 454 | |
Jean Delvare | 8759f90 | 2014-05-12 11:44:51 +0200 | [diff] [blame] | 455 | hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev, |
| 456 | client->name, data, |
| 457 | data->groups); |
Guenter Roeck | 454aee1 | 2013-09-29 11:49:53 -0700 | [diff] [blame] | 458 | if (IS_ERR(hwmon_dev)) |
| 459 | return PTR_ERR(hwmon_dev); |
| 460 | |
Guenter Roeck | 0011ddf | 2013-09-29 13:20:53 -0700 | [diff] [blame] | 461 | dev_info(&client->dev, "%s Thermal chip found\n", id->name); |
Guenter Roeck | 7b52eef | 2012-06-02 09:58:04 -0700 | [diff] [blame] | 462 | return 0; |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | static const unsigned short emc1403_address_list[] = { |
Josef Gajdusek | be7f5c4 | 2014-05-12 14:34:09 +0200 | [diff] [blame] | 466 | 0x18, 0x1c, 0x29, 0x4c, 0x4d, 0x5c, I2C_CLIENT_END |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 467 | }; |
| 468 | |
Josef Gajdusek | be7f5c4 | 2014-05-12 14:34:09 +0200 | [diff] [blame] | 469 | /* Last digit of chip name indicates number of channels */ |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 470 | static const struct i2c_device_id emc1403_idtable[] = { |
Josef Gajdusek | be7f5c4 | 2014-05-12 14:34:09 +0200 | [diff] [blame] | 471 | { "emc1402", emc1402 }, |
| 472 | { "emc1403", emc1403 }, |
| 473 | { "emc1404", emc1404 }, |
Guenter Roeck | 51585be | 2014-05-12 11:46:12 -0700 | [diff] [blame] | 474 | { "emc1412", emc1402 }, |
| 475 | { "emc1413", emc1403 }, |
| 476 | { "emc1414", emc1404 }, |
Josef Gajdusek | be7f5c4 | 2014-05-12 14:34:09 +0200 | [diff] [blame] | 477 | { "emc1422", emc1402 }, |
| 478 | { "emc1423", emc1403 }, |
| 479 | { "emc1424", emc1404 }, |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 480 | { } |
| 481 | }; |
| 482 | MODULE_DEVICE_TABLE(i2c, emc1403_idtable); |
| 483 | |
| 484 | static struct i2c_driver sensor_emc1403 = { |
| 485 | .class = I2C_CLASS_HWMON, |
| 486 | .driver = { |
| 487 | .name = "emc1403", |
| 488 | }, |
| 489 | .detect = emc1403_detect, |
| 490 | .probe = emc1403_probe, |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 491 | .id_table = emc1403_idtable, |
| 492 | .address_list = emc1403_address_list, |
| 493 | }; |
| 494 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 495 | module_i2c_driver(sensor_emc1403); |
Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 496 | |
| 497 | MODULE_AUTHOR("Kalhan Trisal <kalhan.trisal@intel.com"); |
| 498 | MODULE_DESCRIPTION("emc1403 Thermal Driver"); |
| 499 | MODULE_LICENSE("GPL v2"); |