blob: 99ac141bf3d779dcee41c2555957516a8e1db8d7 [file] [log] [blame]
Kalhan Trisaldac68312010-05-27 19:58:56 +02001/*
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
24 * - add emc1404 support
25 */
26
27#include <linux/module.h>
28#include <linux/init.h>
29#include <linux/slab.h>
30#include <linux/i2c.h>
31#include <linux/hwmon.h>
32#include <linux/hwmon-sysfs.h>
33#include <linux/err.h>
34#include <linux/sysfs.h>
35#include <linux/mutex.h>
36
37#define THERMAL_PID_REG 0xfd
38#define THERMAL_SMSC_ID_REG 0xfe
39#define THERMAL_REVISION_REG 0xff
40
41struct thermal_data {
42 struct device *hwmon_dev;
43 struct mutex mutex;
44 /* Cache the hyst value so we don't keep re-reading it. In theory
45 we could cache it forever as nobody else should be writing it. */
46 u8 cached_hyst;
47 unsigned long hyst_valid;
48};
49
50static ssize_t show_temp(struct device *dev,
51 struct device_attribute *attr, char *buf)
52{
53 struct i2c_client *client = to_i2c_client(dev);
54 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
55 int retval = i2c_smbus_read_byte_data(client, sda->index);
56
57 if (retval < 0)
58 return retval;
59 return sprintf(buf, "%d000\n", retval);
60}
61
62static ssize_t show_bit(struct device *dev,
63 struct device_attribute *attr, char *buf)
64{
65 struct i2c_client *client = to_i2c_client(dev);
66 struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr);
67 int retval = i2c_smbus_read_byte_data(client, sda->nr);
68
69 if (retval < 0)
70 return retval;
71 retval &= sda->index;
72 return sprintf(buf, "%d\n", retval ? 1 : 0);
73}
74
75static ssize_t store_temp(struct device *dev,
76 struct device_attribute *attr, const char *buf, size_t count)
77{
78 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
79 struct i2c_client *client = to_i2c_client(dev);
80 unsigned long val;
81 int retval;
82
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +010083 if (kstrtoul(buf, 10, &val))
Kalhan Trisaldac68312010-05-27 19:58:56 +020084 return -EINVAL;
85 retval = i2c_smbus_write_byte_data(client, sda->index,
86 DIV_ROUND_CLOSEST(val, 1000));
87 if (retval < 0)
88 return retval;
89 return count;
90}
91
Alan Cox960f12f2010-08-14 21:08:49 +020092static ssize_t store_bit(struct device *dev,
93 struct device_attribute *attr, const char *buf, size_t count)
94{
95 struct i2c_client *client = to_i2c_client(dev);
96 struct thermal_data *data = i2c_get_clientdata(client);
97 struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr);
98 unsigned long val;
99 int retval;
100
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100101 if (kstrtoul(buf, 10, &val))
Alan Cox960f12f2010-08-14 21:08:49 +0200102 return -EINVAL;
103
104 mutex_lock(&data->mutex);
105 retval = i2c_smbus_read_byte_data(client, sda->nr);
106 if (retval < 0)
107 goto fail;
108
109 retval &= ~sda->index;
110 if (val)
111 retval |= sda->index;
112
113 retval = i2c_smbus_write_byte_data(client, sda->index, retval);
114 if (retval == 0)
115 retval = count;
116fail:
117 mutex_unlock(&data->mutex);
118 return retval;
119}
120
Kalhan Trisaldac68312010-05-27 19:58:56 +0200121static ssize_t show_hyst(struct device *dev,
122 struct device_attribute *attr, char *buf)
123{
124 struct i2c_client *client = to_i2c_client(dev);
125 struct thermal_data *data = i2c_get_clientdata(client);
126 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
127 int retval;
128 int hyst;
129
130 retval = i2c_smbus_read_byte_data(client, sda->index);
131 if (retval < 0)
132 return retval;
133
134 if (time_after(jiffies, data->hyst_valid)) {
135 hyst = i2c_smbus_read_byte_data(client, 0x21);
136 if (hyst < 0)
137 return retval;
138 data->cached_hyst = hyst;
139 data->hyst_valid = jiffies + HZ;
140 }
141 return sprintf(buf, "%d000\n", retval - data->cached_hyst);
142}
143
144static ssize_t store_hyst(struct device *dev,
145 struct device_attribute *attr, const char *buf, size_t count)
146{
147 struct i2c_client *client = to_i2c_client(dev);
148 struct thermal_data *data = i2c_get_clientdata(client);
149 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
150 int retval;
151 int hyst;
152 unsigned long val;
153
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100154 if (kstrtoul(buf, 10, &val))
Kalhan Trisaldac68312010-05-27 19:58:56 +0200155 return -EINVAL;
156
157 mutex_lock(&data->mutex);
158 retval = i2c_smbus_read_byte_data(client, sda->index);
159 if (retval < 0)
160 goto fail;
161
162 hyst = val - retval * 1000;
163 hyst = DIV_ROUND_CLOSEST(hyst, 1000);
164 if (hyst < 0 || hyst > 255) {
165 retval = -ERANGE;
166 goto fail;
167 }
168
169 retval = i2c_smbus_write_byte_data(client, 0x21, hyst);
170 if (retval == 0) {
171 retval = count;
172 data->cached_hyst = hyst;
173 data->hyst_valid = jiffies + HZ;
174 }
175fail:
176 mutex_unlock(&data->mutex);
177 return retval;
178}
179
180/*
181 * Sensors. We pass the actual i2c register to the methods.
182 */
183
184static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO | S_IWUSR,
185 show_temp, store_temp, 0x06);
186static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
187 show_temp, store_temp, 0x05);
188static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
189 show_temp, store_temp, 0x20);
190static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0x00);
191static SENSOR_DEVICE_ATTR_2(temp1_min_alarm, S_IRUGO,
192 show_bit, NULL, 0x36, 0x01);
193static SENSOR_DEVICE_ATTR_2(temp1_max_alarm, S_IRUGO,
194 show_bit, NULL, 0x35, 0x01);
195static SENSOR_DEVICE_ATTR_2(temp1_crit_alarm, S_IRUGO,
196 show_bit, NULL, 0x37, 0x01);
197static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO | S_IWUSR,
198 show_hyst, store_hyst, 0x20);
199
200static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO | S_IWUSR,
201 show_temp, store_temp, 0x08);
202static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR,
203 show_temp, store_temp, 0x07);
204static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO | S_IWUSR,
205 show_temp, store_temp, 0x19);
206static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 0x01);
207static SENSOR_DEVICE_ATTR_2(temp2_min_alarm, S_IRUGO,
208 show_bit, NULL, 0x36, 0x02);
209static SENSOR_DEVICE_ATTR_2(temp2_max_alarm, S_IRUGO,
210 show_bit, NULL, 0x35, 0x02);
211static SENSOR_DEVICE_ATTR_2(temp2_crit_alarm, S_IRUGO,
212 show_bit, NULL, 0x37, 0x02);
213static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO | S_IWUSR,
214 show_hyst, store_hyst, 0x19);
215
216static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO | S_IWUSR,
217 show_temp, store_temp, 0x16);
218static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO | S_IWUSR,
219 show_temp, store_temp, 0x15);
220static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO | S_IWUSR,
221 show_temp, store_temp, 0x1A);
222static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 0x23);
223static SENSOR_DEVICE_ATTR_2(temp3_min_alarm, S_IRUGO,
224 show_bit, NULL, 0x36, 0x04);
225static SENSOR_DEVICE_ATTR_2(temp3_max_alarm, S_IRUGO,
226 show_bit, NULL, 0x35, 0x04);
227static SENSOR_DEVICE_ATTR_2(temp3_crit_alarm, S_IRUGO,
228 show_bit, NULL, 0x37, 0x04);
229static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO | S_IWUSR,
230 show_hyst, store_hyst, 0x1A);
231
Alan Cox960f12f2010-08-14 21:08:49 +0200232static SENSOR_DEVICE_ATTR_2(power_state, S_IRUGO | S_IWUSR,
233 show_bit, store_bit, 0x03, 0x40);
234
Kalhan Trisaldac68312010-05-27 19:58:56 +0200235static struct attribute *mid_att_thermal[] = {
236 &sensor_dev_attr_temp1_min.dev_attr.attr,
237 &sensor_dev_attr_temp1_max.dev_attr.attr,
238 &sensor_dev_attr_temp1_crit.dev_attr.attr,
239 &sensor_dev_attr_temp1_input.dev_attr.attr,
240 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
241 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
242 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
243 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
244 &sensor_dev_attr_temp2_min.dev_attr.attr,
245 &sensor_dev_attr_temp2_max.dev_attr.attr,
246 &sensor_dev_attr_temp2_crit.dev_attr.attr,
247 &sensor_dev_attr_temp2_input.dev_attr.attr,
248 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
249 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
250 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
251 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
252 &sensor_dev_attr_temp3_min.dev_attr.attr,
253 &sensor_dev_attr_temp3_max.dev_attr.attr,
254 &sensor_dev_attr_temp3_crit.dev_attr.attr,
255 &sensor_dev_attr_temp3_input.dev_attr.attr,
256 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
257 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
258 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
259 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
Alan Cox960f12f2010-08-14 21:08:49 +0200260 &sensor_dev_attr_power_state.dev_attr.attr,
Kalhan Trisaldac68312010-05-27 19:58:56 +0200261 NULL
262};
263
264static const struct attribute_group m_thermal_gr = {
265 .attrs = mid_att_thermal
266};
267
268static int emc1403_detect(struct i2c_client *client,
269 struct i2c_board_info *info)
270{
271 int id;
Jekyll Lai7a1b76f2011-01-12 21:55:12 +0100272 /* Check if thermal chip is SMSC and EMC1403 or EMC1423 */
Kalhan Trisaldac68312010-05-27 19:58:56 +0200273
274 id = i2c_smbus_read_byte_data(client, THERMAL_SMSC_ID_REG);
275 if (id != 0x5d)
276 return -ENODEV;
277
Jekyll Lai7a1b76f2011-01-12 21:55:12 +0100278 id = i2c_smbus_read_byte_data(client, THERMAL_PID_REG);
279 switch (id) {
280 case 0x21:
281 strlcpy(info->type, "emc1403", I2C_NAME_SIZE);
282 break;
283 case 0x23:
284 strlcpy(info->type, "emc1423", I2C_NAME_SIZE);
285 break;
Kalhan Trisaldac68312010-05-27 19:58:56 +0200286 /* Note: 0x25 is the 1404 which is very similar and this
287 driver could be extended */
Jekyll Lai7a1b76f2011-01-12 21:55:12 +0100288 default:
Kalhan Trisaldac68312010-05-27 19:58:56 +0200289 return -ENODEV;
Jekyll Lai7a1b76f2011-01-12 21:55:12 +0100290 }
Kalhan Trisaldac68312010-05-27 19:58:56 +0200291
292 id = i2c_smbus_read_byte_data(client, THERMAL_REVISION_REG);
293 if (id != 0x01)
294 return -ENODEV;
295
Kalhan Trisaldac68312010-05-27 19:58:56 +0200296 return 0;
297}
298
299static int emc1403_probe(struct i2c_client *client,
300 const struct i2c_device_id *id)
301{
302 int res;
303 struct thermal_data *data;
304
305 data = kzalloc(sizeof(struct thermal_data), GFP_KERNEL);
306 if (data == NULL) {
307 dev_warn(&client->dev, "out of memory");
308 return -ENOMEM;
309 }
310
311 i2c_set_clientdata(client, data);
312 mutex_init(&data->mutex);
313 data->hyst_valid = jiffies - 1; /* Expired */
314
315 res = sysfs_create_group(&client->dev.kobj, &m_thermal_gr);
316 if (res) {
317 dev_warn(&client->dev, "create group failed\n");
Kalhan Trisaldac68312010-05-27 19:58:56 +0200318 goto thermal_error1;
319 }
320 data->hwmon_dev = hwmon_device_register(&client->dev);
321 if (IS_ERR(data->hwmon_dev)) {
322 res = PTR_ERR(data->hwmon_dev);
323 dev_warn(&client->dev, "register hwmon dev failed\n");
324 goto thermal_error2;
325 }
326 dev_info(&client->dev, "EMC1403 Thermal chip found\n");
327 return res;
328
329thermal_error2:
330 sysfs_remove_group(&client->dev.kobj, &m_thermal_gr);
331thermal_error1:
332 kfree(data);
333 return res;
334}
335
336static int emc1403_remove(struct i2c_client *client)
337{
338 struct thermal_data *data = i2c_get_clientdata(client);
339
340 hwmon_device_unregister(data->hwmon_dev);
341 sysfs_remove_group(&client->dev.kobj, &m_thermal_gr);
342 kfree(data);
343 return 0;
344}
345
346static const unsigned short emc1403_address_list[] = {
Guenter Roeckbcf721d2011-02-09 11:51:29 -0800347 0x18, 0x29, 0x4c, 0x4d, I2C_CLIENT_END
Kalhan Trisaldac68312010-05-27 19:58:56 +0200348};
349
350static const struct i2c_device_id emc1403_idtable[] = {
351 { "emc1403", 0 },
Jekyll Lai7a1b76f2011-01-12 21:55:12 +0100352 { "emc1423", 0 },
Kalhan Trisaldac68312010-05-27 19:58:56 +0200353 { }
354};
355MODULE_DEVICE_TABLE(i2c, emc1403_idtable);
356
357static struct i2c_driver sensor_emc1403 = {
358 .class = I2C_CLASS_HWMON,
359 .driver = {
360 .name = "emc1403",
361 },
362 .detect = emc1403_detect,
363 .probe = emc1403_probe,
364 .remove = emc1403_remove,
365 .id_table = emc1403_idtable,
366 .address_list = emc1403_address_list,
367};
368
Axel Linf0967ee2012-01-20 15:38:18 +0800369module_i2c_driver(sensor_emc1403);
Kalhan Trisaldac68312010-05-27 19:58:56 +0200370
371MODULE_AUTHOR("Kalhan Trisal <kalhan.trisal@intel.com");
372MODULE_DESCRIPTION("emc1403 Thermal Driver");
373MODULE_LICENSE("GPL v2");