1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35#include <linux/module.h>
36#include <linux/init.h>
37#include <linux/slab.h>
38#include <linux/jiffies.h>
39#include <linux/i2c.h>
40#include <linux/hwmon.h>
41#include <linux/hwmon-sysfs.h>
42#include <linux/err.h>
43#include <linux/mutex.h>
44#include <linux/sysfs.h>
45
46static const unsigned short normal_i2c[] = {
47 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
48
49
50
51
52
53#define MAX6642_REG_R_MAN_ID 0xFE
54#define MAX6642_REG_R_CONFIG 0x03
55#define MAX6642_REG_W_CONFIG 0x09
56#define MAX6642_REG_R_STATUS 0x02
57#define MAX6642_REG_R_LOCAL_TEMP 0x00
58#define MAX6642_REG_R_LOCAL_TEMPL 0x11
59#define MAX6642_REG_R_LOCAL_HIGH 0x05
60#define MAX6642_REG_W_LOCAL_HIGH 0x0B
61#define MAX6642_REG_R_REMOTE_TEMP 0x01
62#define MAX6642_REG_R_REMOTE_TEMPL 0x10
63#define MAX6642_REG_R_REMOTE_HIGH 0x07
64#define MAX6642_REG_W_REMOTE_HIGH 0x0D
65
66
67
68
69
70static int temp_from_reg10(int val)
71{
72 return val * 250;
73}
74
75static int temp_from_reg(int val)
76{
77 return val * 1000;
78}
79
80static int temp_to_reg(int val)
81{
82 return val / 1000;
83}
84
85
86
87
88
89struct max6642_data {
90 struct device *hwmon_dev;
91 struct mutex update_lock;
92 bool valid;
93 unsigned long last_updated;
94
95
96 u16 temp_input[2];
97 u16 temp_high[2];
98 u8 alarms;
99};
100
101
102
103
104
105static void max6642_init_client(struct i2c_client *client)
106{
107 u8 config;
108 struct max6642_data *data = i2c_get_clientdata(client);
109
110
111
112
113 config = i2c_smbus_read_byte_data(client, MAX6642_REG_R_CONFIG);
114 if (config & 0x40)
115 i2c_smbus_write_byte_data(client, MAX6642_REG_W_CONFIG,
116 config & 0xBF);
117
118 data->temp_high[0] = i2c_smbus_read_byte_data(client,
119 MAX6642_REG_R_LOCAL_HIGH);
120 data->temp_high[1] = i2c_smbus_read_byte_data(client,
121 MAX6642_REG_R_REMOTE_HIGH);
122}
123
124
125static int max6642_detect(struct i2c_client *client,
126 struct i2c_board_info *info)
127{
128 struct i2c_adapter *adapter = client->adapter;
129 u8 reg_config, reg_status, man_id;
130
131 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
132 return -ENODEV;
133
134
135 man_id = i2c_smbus_read_byte_data(client, MAX6642_REG_R_MAN_ID);
136 if (man_id != 0x4D)
137 return -ENODEV;
138
139
140 if (i2c_smbus_read_byte_data(client, 0x04) != 0x4D
141 || i2c_smbus_read_byte_data(client, 0x06) != 0x4D
142 || i2c_smbus_read_byte_data(client, 0xff) != 0x4D)
143 return -ENODEV;
144
145
146
147
148
149
150 reg_config = i2c_smbus_read_byte_data(client, MAX6642_REG_R_CONFIG);
151 if ((reg_config & 0x0f) != 0x00)
152 return -ENODEV;
153
154
155 if (i2c_smbus_read_byte_data(client, 0x04) != reg_config
156 || i2c_smbus_read_byte_data(client, 0x06) != reg_config
157 || i2c_smbus_read_byte_data(client, 0xff) != reg_config)
158 return -ENODEV;
159
160 reg_status = i2c_smbus_read_byte_data(client, MAX6642_REG_R_STATUS);
161 if ((reg_status & 0x2b) != 0x00)
162 return -ENODEV;
163
164 strlcpy(info->type, "max6642", I2C_NAME_SIZE);
165
166 return 0;
167}
168
169static struct max6642_data *max6642_update_device(struct device *dev)
170{
171 struct i2c_client *client = to_i2c_client(dev);
172 struct max6642_data *data = i2c_get_clientdata(client);
173 u16 val, tmp;
174
175 mutex_lock(&data->update_lock);
176
177 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
178 dev_dbg(&client->dev, "Updating max6642 data.\n");
179 val = i2c_smbus_read_byte_data(client,
180 MAX6642_REG_R_LOCAL_TEMPL);
181 tmp = (val >> 6) & 3;
182 val = i2c_smbus_read_byte_data(client,
183 MAX6642_REG_R_LOCAL_TEMP);
184 val = (val << 2) | tmp;
185 data->temp_input[0] = val;
186 val = i2c_smbus_read_byte_data(client,
187 MAX6642_REG_R_REMOTE_TEMPL);
188 tmp = (val >> 6) & 3;
189 val = i2c_smbus_read_byte_data(client,
190 MAX6642_REG_R_REMOTE_TEMP);
191 val = (val << 2) | tmp;
192 data->temp_input[1] = val;
193 data->alarms = i2c_smbus_read_byte_data(client,
194 MAX6642_REG_R_STATUS);
195
196 data->last_updated = jiffies;
197 data->valid = 1;
198 }
199
200 mutex_unlock(&data->update_lock);
201
202 return data;
203}
204
205
206
207
208
209static ssize_t show_temp_max10(struct device *dev,
210 struct device_attribute *dev_attr, char *buf)
211{
212 struct max6642_data *data = max6642_update_device(dev);
213 struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
214
215 return sprintf(buf, "%d\n",
216 temp_from_reg10(data->temp_input[attr->index]));
217}
218
219static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
220 char *buf)
221{
222 struct max6642_data *data = max6642_update_device(dev);
223 struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr);
224
225 return sprintf(buf, "%d\n", temp_from_reg(data->temp_high[attr2->nr]));
226}
227
228static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
229 const char *buf, size_t count)
230{
231 unsigned long val;
232 int err;
233 struct i2c_client *client = to_i2c_client(dev);
234 struct max6642_data *data = i2c_get_clientdata(client);
235 struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr);
236
237 err = strict_strtoul(buf, 10, &val);
238 if (err < 0)
239 return err;
240
241 mutex_lock(&data->update_lock);
242 data->temp_high[attr2->nr] = SENSORS_LIMIT(temp_to_reg(val), 0, 255);
243 i2c_smbus_write_byte_data(client, attr2->index,
244 data->temp_high[attr2->nr]);
245 mutex_unlock(&data->update_lock);
246 return count;
247}
248
249static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
250 char *buf)
251{
252 int bitnr = to_sensor_dev_attr(attr)->index;
253 struct max6642_data *data = max6642_update_device(dev);
254 return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
255}
256
257static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_max10, NULL, 0);
258static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_max10, NULL, 1);
259static SENSOR_DEVICE_ATTR_2(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
260 set_temp_max, 0, MAX6642_REG_W_LOCAL_HIGH);
261static SENSOR_DEVICE_ATTR_2(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
262 set_temp_max, 1, MAX6642_REG_W_REMOTE_HIGH);
263static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
264static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
265static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
266
267static struct attribute *max6642_attributes[] = {
268 &sensor_dev_attr_temp1_input.dev_attr.attr,
269 &sensor_dev_attr_temp2_input.dev_attr.attr,
270 &sensor_dev_attr_temp1_max.dev_attr.attr,
271 &sensor_dev_attr_temp2_max.dev_attr.attr,
272
273 &sensor_dev_attr_temp2_fault.dev_attr.attr,
274 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
275 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
276 NULL
277};
278
279static const struct attribute_group max6642_group = {
280 .attrs = max6642_attributes,
281};
282
283static int max6642_probe(struct i2c_client *new_client,
284 const struct i2c_device_id *id)
285{
286 struct max6642_data *data;
287 int err;
288
289 data = kzalloc(sizeof(struct max6642_data), GFP_KERNEL);
290 if (!data) {
291 err = -ENOMEM;
292 goto exit;
293 }
294
295 i2c_set_clientdata(new_client, data);
296 mutex_init(&data->update_lock);
297
298
299 max6642_init_client(new_client);
300
301
302 err = sysfs_create_group(&new_client->dev.kobj, &max6642_group);
303 if (err)
304 goto exit_free;
305
306 data->hwmon_dev = hwmon_device_register(&new_client->dev);
307 if (IS_ERR(data->hwmon_dev)) {
308 err = PTR_ERR(data->hwmon_dev);
309 goto exit_remove_files;
310 }
311
312 return 0;
313
314exit_remove_files:
315 sysfs_remove_group(&new_client->dev.kobj, &max6642_group);
316exit_free:
317 kfree(data);
318exit:
319 return err;
320}
321
322static int max6642_remove(struct i2c_client *client)
323{
324 struct max6642_data *data = i2c_get_clientdata(client);
325
326 hwmon_device_unregister(data->hwmon_dev);
327 sysfs_remove_group(&client->dev.kobj, &max6642_group);
328
329 kfree(data);
330 return 0;
331}
332
333
334
335
336
337static const struct i2c_device_id max6642_id[] = {
338 { "max6642", 0 },
339 { }
340};
341MODULE_DEVICE_TABLE(i2c, max6642_id);
342
343static struct i2c_driver max6642_driver = {
344 .class = I2C_CLASS_HWMON,
345 .driver = {
346 .name = "max6642",
347 },
348 .probe = max6642_probe,
349 .remove = max6642_remove,
350 .id_table = max6642_id,
351 .detect = max6642_detect,
352 .address_list = normal_i2c,
353};
354
355static int __init max6642_init(void)
356{
357 return i2c_add_driver(&max6642_driver);
358}
359
360static void __exit max6642_exit(void)
361{
362 i2c_del_driver(&max6642_driver);
363}
364
365MODULE_AUTHOR("Per Dalen <per.dalen@appeartv.com>");
366MODULE_DESCRIPTION("MAX6642 sensor driver");
367MODULE_LICENSE("GPL");
368
369module_init(max6642_init);
370module_exit(max6642_exit);
371