2 * Maxim MAX197 A/D Converter driver
4 * Copyright (c) 2012 Savoir-faire Linux Inc.
5 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * For further information, see the Documentation/hwmon/max197 file.
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/err.h>
18 #include <linux/slab.h>
19 #include <linux/mutex.h>
20 #include <linux/device.h>
21 #include <linux/sysfs.h>
22 #include <linux/hwmon.h>
23 #include <linux/hwmon-sysfs.h>
24 #include <linux/platform_device.h>
25 #include <linux/platform_data/max197.h>
27 #define MAX199_LIMIT 4000 /* 4V */
28 #define MAX197_LIMIT 10000 /* 10V */
30 #define MAX197_NUM_CH 8 /* 8 Analog Input Channels */
32 /* Control byte format */
33 #define MAX197_BIP (1 << 3) /* Bipolarity */
34 #define MAX197_RNG (1 << 4) /* Full range */
36 #define MAX197_SCALE 12207 /* Scale coefficient for raw data */
38 /* List of supported chips */
39 enum max197_chips
{ max197
, max199
};
42 * struct max197_data - device instance specific data
43 * @pdata: Platform data.
44 * @hwmon_dev: The hwmon device.
45 * @lock: Read/Write mutex.
46 * @limit: Max range value (10V for MAX197, 4V for MAX199).
47 * @scale: Need to scale.
48 * @ctrl_bytes: Channels control byte.
51 struct max197_platform_data
*pdata
;
52 struct device
*hwmon_dev
;
56 u8 ctrl_bytes
[MAX197_NUM_CH
];
59 static inline void max197_set_unipolarity(struct max197_data
*data
, int channel
)
61 data
->ctrl_bytes
[channel
] &= ~MAX197_BIP
;
64 static inline void max197_set_bipolarity(struct max197_data
*data
, int channel
)
66 data
->ctrl_bytes
[channel
] |= MAX197_BIP
;
69 static inline void max197_set_half_range(struct max197_data
*data
, int channel
)
71 data
->ctrl_bytes
[channel
] &= ~MAX197_RNG
;
74 static inline void max197_set_full_range(struct max197_data
*data
, int channel
)
76 data
->ctrl_bytes
[channel
] |= MAX197_RNG
;
79 static inline bool max197_is_bipolar(struct max197_data
*data
, int channel
)
81 return data
->ctrl_bytes
[channel
] & MAX197_BIP
;
84 static inline bool max197_is_full_range(struct max197_data
*data
, int channel
)
86 return data
->ctrl_bytes
[channel
] & MAX197_RNG
;
89 /* Function called on read access on in{0,1,2,3,4,5,6,7}_{min,max} */
90 static ssize_t
max197_show_range(struct device
*dev
,
91 struct device_attribute
*devattr
, char *buf
)
93 struct max197_data
*data
= dev_get_drvdata(dev
);
94 struct sensor_device_attribute_2
*attr
= to_sensor_dev_attr_2(devattr
);
95 int channel
= attr
->index
;
96 bool is_min
= attr
->nr
;
99 if (mutex_lock_interruptible(&data
->lock
))
102 range
= max197_is_full_range(data
, channel
) ?
103 data
->limit
: data
->limit
/ 2;
105 if (max197_is_bipolar(data
, channel
))
111 mutex_unlock(&data
->lock
);
113 return sprintf(buf
, "%d\n", range
);
116 /* Function called on write access on in{0,1,2,3,4,5,6,7}_{min,max} */
117 static ssize_t
max197_store_range(struct device
*dev
,
118 struct device_attribute
*devattr
,
119 const char *buf
, size_t count
)
121 struct max197_data
*data
= dev_get_drvdata(dev
);
122 struct sensor_device_attribute_2
*attr
= to_sensor_dev_attr_2(devattr
);
123 int channel
= attr
->index
;
124 bool is_min
= attr
->nr
;
126 int half
= data
->limit
/ 2;
127 int full
= data
->limit
;
129 if (kstrtol(buf
, 10, &value
))
146 if (mutex_lock_interruptible(&data
->lock
))
150 /* We can deduce only the polarity */
151 max197_set_unipolarity(data
, channel
);
152 } else if (value
== -half
) {
153 max197_set_bipolarity(data
, channel
);
154 max197_set_half_range(data
, channel
);
155 } else if (value
== -full
) {
156 max197_set_bipolarity(data
, channel
);
157 max197_set_full_range(data
, channel
);
158 } else if (value
== half
) {
159 /* We can deduce only the range */
160 max197_set_half_range(data
, channel
);
161 } else if (value
== full
) {
162 /* We can deduce only the range */
163 max197_set_full_range(data
, channel
);
166 mutex_unlock(&data
->lock
);
171 /* Function called on read access on in{0,1,2,3,4,5,6,7}_input */
172 static ssize_t
max197_show_input(struct device
*dev
,
173 struct device_attribute
*devattr
,
176 struct max197_data
*data
= dev_get_drvdata(dev
);
177 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
178 int channel
= attr
->index
;
182 if (mutex_lock_interruptible(&data
->lock
))
185 ret
= data
->pdata
->convert(data
->ctrl_bytes
[channel
]);
187 dev_err(dev
, "conversion failed\n");
193 * Coefficient to apply on raw value.
194 * See Table 1. Full Scale and Zero Scale in the MAX197 datasheet.
197 value
*= MAX197_SCALE
;
198 if (max197_is_full_range(data
, channel
))
203 ret
= sprintf(buf
, "%d\n", value
);
206 mutex_unlock(&data
->lock
);
210 static ssize_t
max197_show_name(struct device
*dev
,
211 struct device_attribute
*attr
, char *buf
)
213 struct platform_device
*pdev
= to_platform_device(dev
);
214 return sprintf(buf
, "%s\n", pdev
->name
);
217 #define MAX197_SENSOR_DEVICE_ATTR_CH(chan) \
218 static SENSOR_DEVICE_ATTR(in##chan##_input, S_IRUGO, \
219 max197_show_input, NULL, chan); \
220 static SENSOR_DEVICE_ATTR_2(in##chan##_min, S_IRUGO | S_IWUSR, \
222 max197_store_range, \
224 static SENSOR_DEVICE_ATTR_2(in##chan##_max, S_IRUGO | S_IWUSR, \
226 max197_store_range, \
229 #define MAX197_SENSOR_DEV_ATTR_IN(chan) \
230 &sensor_dev_attr_in##chan##_input.dev_attr.attr, \
231 &sensor_dev_attr_in##chan##_max.dev_attr.attr, \
232 &sensor_dev_attr_in##chan##_min.dev_attr.attr
234 static DEVICE_ATTR(name
, S_IRUGO
, max197_show_name
, NULL
);
236 MAX197_SENSOR_DEVICE_ATTR_CH(0);
237 MAX197_SENSOR_DEVICE_ATTR_CH(1);
238 MAX197_SENSOR_DEVICE_ATTR_CH(2);
239 MAX197_SENSOR_DEVICE_ATTR_CH(3);
240 MAX197_SENSOR_DEVICE_ATTR_CH(4);
241 MAX197_SENSOR_DEVICE_ATTR_CH(5);
242 MAX197_SENSOR_DEVICE_ATTR_CH(6);
243 MAX197_SENSOR_DEVICE_ATTR_CH(7);
245 static const struct attribute_group max197_sysfs_group
= {
246 .attrs
= (struct attribute
*[]) {
248 MAX197_SENSOR_DEV_ATTR_IN(0),
249 MAX197_SENSOR_DEV_ATTR_IN(1),
250 MAX197_SENSOR_DEV_ATTR_IN(2),
251 MAX197_SENSOR_DEV_ATTR_IN(3),
252 MAX197_SENSOR_DEV_ATTR_IN(4),
253 MAX197_SENSOR_DEV_ATTR_IN(5),
254 MAX197_SENSOR_DEV_ATTR_IN(6),
255 MAX197_SENSOR_DEV_ATTR_IN(7),
260 static int max197_probe(struct platform_device
*pdev
)
263 struct max197_data
*data
;
264 struct max197_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
265 enum max197_chips chip
= platform_get_device_id(pdev
)->driver_data
;
268 dev_err(&pdev
->dev
, "no platform data supplied\n");
272 if (pdata
->convert
== NULL
) {
273 dev_err(&pdev
->dev
, "no convert function supplied\n");
277 data
= devm_kzalloc(&pdev
->dev
, sizeof(struct max197_data
), GFP_KERNEL
);
282 mutex_init(&data
->lock
);
284 if (chip
== max197
) {
285 data
->limit
= MAX197_LIMIT
;
288 data
->limit
= MAX199_LIMIT
;
292 for (ch
= 0; ch
< MAX197_NUM_CH
; ch
++)
293 data
->ctrl_bytes
[ch
] = (u8
) ch
;
295 platform_set_drvdata(pdev
, data
);
297 ret
= sysfs_create_group(&pdev
->dev
.kobj
, &max197_sysfs_group
);
299 dev_err(&pdev
->dev
, "sysfs create group failed\n");
303 data
->hwmon_dev
= hwmon_device_register(&pdev
->dev
);
304 if (IS_ERR(data
->hwmon_dev
)) {
305 ret
= PTR_ERR(data
->hwmon_dev
);
306 dev_err(&pdev
->dev
, "hwmon device register failed\n");
313 sysfs_remove_group(&pdev
->dev
.kobj
, &max197_sysfs_group
);
317 static int max197_remove(struct platform_device
*pdev
)
319 struct max197_data
*data
= platform_get_drvdata(pdev
);
321 hwmon_device_unregister(data
->hwmon_dev
);
322 sysfs_remove_group(&pdev
->dev
.kobj
, &max197_sysfs_group
);
327 static const struct platform_device_id max197_device_ids
[] = {
328 { "max197", max197
},
329 { "max199", max199
},
332 MODULE_DEVICE_TABLE(platform
, max197_device_ids
);
334 static struct platform_driver max197_driver
= {
338 .probe
= max197_probe
,
339 .remove
= max197_remove
,
340 .id_table
= max197_device_ids
,
342 module_platform_driver(max197_driver
);
344 MODULE_LICENSE("GPL");
345 MODULE_AUTHOR("Savoir-faire Linux Inc. <kernel@savoirfairelinux.com>");
346 MODULE_DESCRIPTION("Maxim MAX197 A/D Converter driver");