Blame view

drivers/hwmon/max197.c 8.52 KB
d2912cb15   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
6c1fe725f   Vivien Didelot   hwmon: add Maxim ...
2
3
4
5
6
7
  /*
   * Maxim MAX197 A/D Converter driver
   *
   * Copyright (c) 2012 Savoir-faire Linux Inc.
   *          Vivien Didelot <vivien.didelot@savoirfairelinux.com>
   *
7ebd8b66d   Mauro Carvalho Chehab   docs: hwmon: Add ...
8
   * For further information, see the Documentation/hwmon/max197.rst file.
6c1fe725f   Vivien Didelot   hwmon: add Maxim ...
9
10
11
12
   */
  
  #include <linux/kernel.h>
  #include <linux/module.h>
ac3167257   Randy Dunlap   headers: separate...
13
  #include <linux/mod_devicetable.h>
6c1fe725f   Vivien Didelot   hwmon: add Maxim ...
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
  #include <linux/init.h>
  #include <linux/err.h>
  #include <linux/slab.h>
  #include <linux/mutex.h>
  #include <linux/device.h>
  #include <linux/sysfs.h>
  #include <linux/hwmon.h>
  #include <linux/hwmon-sysfs.h>
  #include <linux/platform_device.h>
  #include <linux/platform_data/max197.h>
  
  #define MAX199_LIMIT	4000		/* 4V */
  #define MAX197_LIMIT	10000		/* 10V */
  
  #define MAX197_NUM_CH	8		/* 8 Analog Input Channels */
  
  /* Control byte format */
  #define MAX197_BIP	(1 << 3)	/* Bipolarity */
  #define MAX197_RNG	(1 << 4)	/* Full range */
  
  #define MAX197_SCALE	12207		/* Scale coefficient for raw data */
  
  /* List of supported chips */
  enum max197_chips { max197, max199 };
  
  /**
   * struct max197_data - device instance specific data
   * @pdata:		Platform data.
   * @hwmon_dev:		The hwmon device.
   * @lock:		Read/Write mutex.
   * @limit:		Max range value (10V for MAX197, 4V for MAX199).
   * @scale:		Need to scale.
   * @ctrl_bytes:		Channels control byte.
   */
  struct max197_data {
  	struct max197_platform_data *pdata;
  	struct device *hwmon_dev;
  	struct mutex lock;
  	int limit;
  	bool scale;
  	u8 ctrl_bytes[MAX197_NUM_CH];
  };
  
  static inline void max197_set_unipolarity(struct max197_data *data, int channel)
  {
  	data->ctrl_bytes[channel] &= ~MAX197_BIP;
  }
  
  static inline void max197_set_bipolarity(struct max197_data *data, int channel)
  {
  	data->ctrl_bytes[channel] |= MAX197_BIP;
  }
  
  static inline void max197_set_half_range(struct max197_data *data, int channel)
  {
  	data->ctrl_bytes[channel] &= ~MAX197_RNG;
  }
  
  static inline void max197_set_full_range(struct max197_data *data, int channel)
  {
  	data->ctrl_bytes[channel] |= MAX197_RNG;
  }
  
  static inline bool max197_is_bipolar(struct max197_data *data, int channel)
  {
  	return data->ctrl_bytes[channel] & MAX197_BIP;
  }
  
  static inline bool max197_is_full_range(struct max197_data *data, int channel)
  {
  	return data->ctrl_bytes[channel] & MAX197_RNG;
  }
  
  /* Function called on read access on in{0,1,2,3,4,5,6,7}_{min,max} */
  static ssize_t max197_show_range(struct device *dev,
  				 struct device_attribute *devattr, char *buf)
  {
  	struct max197_data *data = dev_get_drvdata(dev);
  	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  	int channel = attr->index;
  	bool is_min = attr->nr;
  	int range;
  
  	if (mutex_lock_interruptible(&data->lock))
  		return -ERESTARTSYS;
  
  	range = max197_is_full_range(data, channel) ?
  		data->limit : data->limit / 2;
  	if (is_min) {
  		if (max197_is_bipolar(data, channel))
  			range = -range;
  		else
  			range = 0;
  	}
  
  	mutex_unlock(&data->lock);
  
  	return sprintf(buf, "%d
  ", range);
  }
  
  /* Function called on write access on in{0,1,2,3,4,5,6,7}_{min,max} */
  static ssize_t max197_store_range(struct device *dev,
  				  struct device_attribute *devattr,
  				  const char *buf, size_t count)
  {
  	struct max197_data *data = dev_get_drvdata(dev);
  	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  	int channel = attr->index;
  	bool is_min = attr->nr;
  	long value;
  	int half = data->limit / 2;
  	int full = data->limit;
  
  	if (kstrtol(buf, 10, &value))
  		return -EINVAL;
  
  	if (is_min) {
  		if (value <= -full)
  			value = -full;
  		else if (value < 0)
  			value = -half;
  		else
  			value = 0;
  	} else {
  		if (value >= full)
  			value = full;
  		else
  			value = half;
  	}
  
  	if (mutex_lock_interruptible(&data->lock))
  		return -ERESTARTSYS;
  
  	if (value == 0) {
  		/* We can deduce only the polarity */
  		max197_set_unipolarity(data, channel);
  	} else if (value == -half) {
  		max197_set_bipolarity(data, channel);
  		max197_set_half_range(data, channel);
  	} else if (value == -full) {
  		max197_set_bipolarity(data, channel);
  		max197_set_full_range(data, channel);
  	} else if (value == half) {
  		/* We can deduce only the range */
  		max197_set_half_range(data, channel);
  	} else if (value == full) {
  		/* We can deduce only the range */
  		max197_set_full_range(data, channel);
  	}
  
  	mutex_unlock(&data->lock);
  
  	return count;
  }
  
  /* Function called on read access on in{0,1,2,3,4,5,6,7}_input */
  static ssize_t max197_show_input(struct device *dev,
  				 struct device_attribute *devattr,
  				 char *buf)
  {
  	struct max197_data *data = dev_get_drvdata(dev);
  	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  	int channel = attr->index;
  	s32 value;
  	int ret;
  
  	if (mutex_lock_interruptible(&data->lock))
  		return -ERESTARTSYS;
  
  	ret = data->pdata->convert(data->ctrl_bytes[channel]);
  	if (ret < 0) {
  		dev_err(dev, "conversion failed
  ");
  		goto unlock;
  	}
  	value = ret;
  
  	/*
  	 * Coefficient to apply on raw value.
  	 * See Table 1. Full Scale and Zero Scale in the MAX197 datasheet.
  	 */
  	if (data->scale) {
  		value *= MAX197_SCALE;
  		if (max197_is_full_range(data, channel))
  			value *= 2;
  		value /= 10000;
  	}
  
  	ret = sprintf(buf, "%d
  ", value);
  
  unlock:
  	mutex_unlock(&data->lock);
  	return ret;
  }
a93c843bb   Julia Lawall   hwmon: (max197) u...
210
211
  static ssize_t name_show(struct device *dev, struct device_attribute *attr,
  			 char *buf)
6c1fe725f   Vivien Didelot   hwmon: add Maxim ...
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
  {
  	struct platform_device *pdev = to_platform_device(dev);
  	return sprintf(buf, "%s
  ", pdev->name);
  }
  
  #define MAX197_SENSOR_DEVICE_ATTR_CH(chan)				\
  	static SENSOR_DEVICE_ATTR(in##chan##_input, S_IRUGO,		\
  				  max197_show_input, NULL, chan);	\
  	static SENSOR_DEVICE_ATTR_2(in##chan##_min, S_IRUGO | S_IWUSR,	\
  				    max197_show_range,			\
  				    max197_store_range,			\
  				    true, chan);			\
  	static SENSOR_DEVICE_ATTR_2(in##chan##_max, S_IRUGO | S_IWUSR,	\
  				    max197_show_range,			\
  				    max197_store_range,			\
  				    false, chan)
  
  #define MAX197_SENSOR_DEV_ATTR_IN(chan)					\
  	&sensor_dev_attr_in##chan##_input.dev_attr.attr,		\
  	&sensor_dev_attr_in##chan##_max.dev_attr.attr,			\
  	&sensor_dev_attr_in##chan##_min.dev_attr.attr
a93c843bb   Julia Lawall   hwmon: (max197) u...
234
  static DEVICE_ATTR_RO(name);
6c1fe725f   Vivien Didelot   hwmon: add Maxim ...
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
  
  MAX197_SENSOR_DEVICE_ATTR_CH(0);
  MAX197_SENSOR_DEVICE_ATTR_CH(1);
  MAX197_SENSOR_DEVICE_ATTR_CH(2);
  MAX197_SENSOR_DEVICE_ATTR_CH(3);
  MAX197_SENSOR_DEVICE_ATTR_CH(4);
  MAX197_SENSOR_DEVICE_ATTR_CH(5);
  MAX197_SENSOR_DEVICE_ATTR_CH(6);
  MAX197_SENSOR_DEVICE_ATTR_CH(7);
  
  static const struct attribute_group max197_sysfs_group = {
  	.attrs = (struct attribute *[]) {
  		&dev_attr_name.attr,
  		MAX197_SENSOR_DEV_ATTR_IN(0),
  		MAX197_SENSOR_DEV_ATTR_IN(1),
  		MAX197_SENSOR_DEV_ATTR_IN(2),
  		MAX197_SENSOR_DEV_ATTR_IN(3),
  		MAX197_SENSOR_DEV_ATTR_IN(4),
  		MAX197_SENSOR_DEV_ATTR_IN(5),
  		MAX197_SENSOR_DEV_ATTR_IN(6),
  		MAX197_SENSOR_DEV_ATTR_IN(7),
  		NULL
  	},
  };
6c931ae1c   Bill Pemberton   hwmon: remove use...
259
  static int max197_probe(struct platform_device *pdev)
6c1fe725f   Vivien Didelot   hwmon: add Maxim ...
260
261
262
  {
  	int ch, ret;
  	struct max197_data *data;
a8b3a3a53   Jingoo Han   hwmon: use dev_ge...
263
  	struct max197_platform_data *pdata = dev_get_platdata(&pdev->dev);
6c1fe725f   Vivien Didelot   hwmon: add Maxim ...
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
  	enum max197_chips chip = platform_get_device_id(pdev)->driver_data;
  
  	if (pdata == NULL) {
  		dev_err(&pdev->dev, "no platform data supplied
  ");
  		return -EINVAL;
  	}
  
  	if (pdata->convert == NULL) {
  		dev_err(&pdev->dev, "no convert function supplied
  ");
  		return -EINVAL;
  	}
  
  	data = devm_kzalloc(&pdev->dev, sizeof(struct max197_data), GFP_KERNEL);
6122de8f8   Jingoo Han   hwmon: (max197) r...
279
  	if (!data)
6c1fe725f   Vivien Didelot   hwmon: add Maxim ...
280
  		return -ENOMEM;
6c1fe725f   Vivien Didelot   hwmon: add Maxim ...
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
  
  	data->pdata = pdata;
  	mutex_init(&data->lock);
  
  	if (chip == max197) {
  		data->limit = MAX197_LIMIT;
  		data->scale = true;
  	} else {
  		data->limit = MAX199_LIMIT;
  		data->scale = false;
  	}
  
  	for (ch = 0; ch < MAX197_NUM_CH; ch++)
  		data->ctrl_bytes[ch] = (u8) ch;
  
  	platform_set_drvdata(pdev, data);
  
  	ret = sysfs_create_group(&pdev->dev.kobj, &max197_sysfs_group);
  	if (ret) {
  		dev_err(&pdev->dev, "sysfs create group failed
  ");
  		return ret;
  	}
  
  	data->hwmon_dev = hwmon_device_register(&pdev->dev);
  	if (IS_ERR(data->hwmon_dev)) {
  		ret = PTR_ERR(data->hwmon_dev);
  		dev_err(&pdev->dev, "hwmon device register failed
  ");
  		goto error;
  	}
  
  	return 0;
  
  error:
  	sysfs_remove_group(&pdev->dev.kobj, &max197_sysfs_group);
  	return ret;
  }
281dfd0b6   Bill Pemberton   hwmon: remove use...
319
  static int max197_remove(struct platform_device *pdev)
6c1fe725f   Vivien Didelot   hwmon: add Maxim ...
320
321
322
323
324
325
326
327
  {
  	struct max197_data *data = platform_get_drvdata(pdev);
  
  	hwmon_device_unregister(data->hwmon_dev);
  	sysfs_remove_group(&pdev->dev.kobj, &max197_sysfs_group);
  
  	return 0;
  }
9c2cbcec8   Krzysztof Kozlowski   hwmon: (max197) C...
328
  static const struct platform_device_id max197_device_ids[] = {
6c1fe725f   Vivien Didelot   hwmon: add Maxim ...
329
330
331
332
333
334
335
336
337
  	{ "max197", max197 },
  	{ "max199", max199 },
  	{ }
  };
  MODULE_DEVICE_TABLE(platform, max197_device_ids);
  
  static struct platform_driver max197_driver = {
  	.driver = {
  		.name = "max197",
6c1fe725f   Vivien Didelot   hwmon: add Maxim ...
338
339
  	},
  	.probe = max197_probe,
9e5e9b7a9   Bill Pemberton   hwmon: remove use...
340
  	.remove = max197_remove,
6c1fe725f   Vivien Didelot   hwmon: add Maxim ...
341
342
343
344
345
346
347
  	.id_table = max197_device_ids,
  };
  module_platform_driver(max197_driver);
  
  MODULE_LICENSE("GPL");
  MODULE_AUTHOR("Savoir-faire Linux Inc. <kernel@savoirfairelinux.com>");
  MODULE_DESCRIPTION("Maxim MAX197 A/D Converter driver");