Linux Audio

Check our new training course

Loading...
v4.10.11
  1/*
  2 *  Driver for the ADT7411 (I2C/SPI 8 channel 10 bit ADC & temperature-sensor)
  3 *
  4 *  Copyright (C) 2008, 2010 Pengutronix
  5 *
  6 *  This program is free software; you can redistribute it and/or modify
  7 *  it under the terms of the GNU General Public License version 2 as
  8 *  published by the Free Software Foundation.
  9 *
 10 *  TODO: SPI, use power-down mode for suspend?, interrupt handling?
 11 */
 12
 13#include <linux/kernel.h>
 14#include <linux/module.h>
 15#include <linux/init.h>
 16#include <linux/err.h>
 17#include <linux/mutex.h>
 18#include <linux/jiffies.h>
 19#include <linux/i2c.h>
 20#include <linux/hwmon.h>
 21#include <linux/hwmon-sysfs.h>
 22#include <linux/slab.h>
 23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 24#define ADT7411_REG_INT_TEMP_VDD_LSB		0x03
 25#define ADT7411_REG_EXT_TEMP_AIN14_LSB		0x04
 26#define ADT7411_REG_VDD_MSB			0x06
 27#define ADT7411_REG_INT_TEMP_MSB		0x07
 28#define ADT7411_REG_EXT_TEMP_AIN1_MSB		0x08
 29
 30#define ADT7411_REG_CFG1			0x18
 31#define ADT7411_CFG1_START_MONITOR		(1 << 0)
 32#define ADT7411_CFG1_RESERVED_BIT1		(1 << 1)
 33#define ADT7411_CFG1_EXT_TDM			(1 << 2)
 34#define ADT7411_CFG1_RESERVED_BIT3		(1 << 3)
 35
 36#define ADT7411_REG_CFG2			0x19
 37#define ADT7411_CFG2_DISABLE_AVG		(1 << 5)
 38
 39#define ADT7411_REG_CFG3			0x1a
 40#define ADT7411_CFG3_ADC_CLK_225		(1 << 0)
 41#define ADT7411_CFG3_RESERVED_BIT1		(1 << 1)
 42#define ADT7411_CFG3_RESERVED_BIT2		(1 << 2)
 43#define ADT7411_CFG3_RESERVED_BIT3		(1 << 3)
 44#define ADT7411_CFG3_REF_VDD			(1 << 4)
 
 
 
 
 
 
 
 
 
 
 
 45
 46#define ADT7411_REG_DEVICE_ID			0x4d
 47#define ADT7411_REG_MANUFACTURER_ID		0x4e
 48
 49#define ADT7411_DEVICE_ID			0x2
 50#define ADT7411_MANUFACTURER_ID			0x41
 51
 52static const unsigned short normal_i2c[] = { 0x48, 0x4a, 0x4b, I2C_CLIENT_END };
 53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 54struct adt7411_data {
 55	struct mutex device_lock;	/* for "atomic" device accesses */
 56	struct mutex update_lock;
 57	unsigned long next_update;
 58	long vref_cached;
 59	struct i2c_client *client;
 60	bool use_ext_temp;
 61};
 62
 63/*
 64 * When reading a register containing (up to 4) lsb, all associated
 65 * msb-registers get locked by the hardware. After _one_ of those msb is read,
 66 * _all_ are unlocked. In order to use this locking correctly, reading lsb/msb
 67 * is protected here with a mutex, too.
 68 */
 69static int adt7411_read_10_bit(struct i2c_client *client, u8 lsb_reg,
 70				u8 msb_reg, u8 lsb_shift)
 71{
 72	struct adt7411_data *data = i2c_get_clientdata(client);
 73	int val, tmp;
 74
 75	mutex_lock(&data->device_lock);
 76
 77	val = i2c_smbus_read_byte_data(client, lsb_reg);
 78	if (val < 0)
 79		goto exit_unlock;
 80
 81	tmp = (val >> lsb_shift) & 3;
 82	val = i2c_smbus_read_byte_data(client, msb_reg);
 83
 84	if (val >= 0)
 85		val = (val << 2) | tmp;
 86
 87 exit_unlock:
 88	mutex_unlock(&data->device_lock);
 89
 90	return val;
 91}
 92
 93static int adt7411_modify_bit(struct i2c_client *client, u8 reg, u8 bit,
 94				bool flag)
 95{
 96	struct adt7411_data *data = i2c_get_clientdata(client);
 97	int ret, val;
 98
 99	mutex_lock(&data->device_lock);
100
101	ret = i2c_smbus_read_byte_data(client, reg);
102	if (ret < 0)
103		goto exit_unlock;
104
105	if (flag)
106		val = ret | bit;
107	else
108		val = ret & ~bit;
109
110	ret = i2c_smbus_write_byte_data(client, reg, val);
111
112 exit_unlock:
113	mutex_unlock(&data->device_lock);
114	return ret;
115}
116
117static ssize_t adt7411_show_bit(struct device *dev,
118				struct device_attribute *attr, char *buf)
119{
120	struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr);
121	struct adt7411_data *data = dev_get_drvdata(dev);
122	struct i2c_client *client = data->client;
123	int ret = i2c_smbus_read_byte_data(client, attr2->index);
124
125	return ret < 0 ? ret : sprintf(buf, "%u\n", !!(ret & attr2->nr));
126}
127
128static ssize_t adt7411_set_bit(struct device *dev,
129			       struct device_attribute *attr, const char *buf,
130			       size_t count)
131{
132	struct sensor_device_attribute_2 *s_attr2 = to_sensor_dev_attr_2(attr);
133	struct adt7411_data *data = dev_get_drvdata(dev);
134	struct i2c_client *client = data->client;
135	int ret;
136	unsigned long flag;
137
138	ret = kstrtoul(buf, 0, &flag);
139	if (ret || flag > 1)
140		return -EINVAL;
141
142	ret = adt7411_modify_bit(client, s_attr2->index, s_attr2->nr, flag);
143
144	/* force update */
145	mutex_lock(&data->update_lock);
146	data->next_update = jiffies;
147	mutex_unlock(&data->update_lock);
148
149	return ret < 0 ? ret : count;
150}
151
152#define ADT7411_BIT_ATTR(__name, __reg, __bit) \
153	SENSOR_DEVICE_ATTR_2(__name, S_IRUGO | S_IWUSR, adt7411_show_bit, \
154	adt7411_set_bit, __bit, __reg)
155
156static ADT7411_BIT_ATTR(no_average, ADT7411_REG_CFG2, ADT7411_CFG2_DISABLE_AVG);
157static ADT7411_BIT_ATTR(fast_sampling, ADT7411_REG_CFG3, ADT7411_CFG3_ADC_CLK_225);
158static ADT7411_BIT_ATTR(adc_ref_vdd, ADT7411_REG_CFG3, ADT7411_CFG3_REF_VDD);
159
160static struct attribute *adt7411_attrs[] = {
161	&sensor_dev_attr_no_average.dev_attr.attr,
162	&sensor_dev_attr_fast_sampling.dev_attr.attr,
163	&sensor_dev_attr_adc_ref_vdd.dev_attr.attr,
164	NULL
165};
166ATTRIBUTE_GROUPS(adt7411);
167
 
 
 
 
 
 
 
 
 
 
 
 
 
168static int adt7411_read_in_vdd(struct device *dev, u32 attr, long *val)
169{
170	struct adt7411_data *data = dev_get_drvdata(dev);
171	struct i2c_client *client = data->client;
172	int ret;
173
174	switch (attr) {
175	case hwmon_in_input:
176		ret = adt7411_read_10_bit(client, ADT7411_REG_INT_TEMP_VDD_LSB,
177					  ADT7411_REG_VDD_MSB, 2);
178		if (ret < 0)
179			return ret;
180		*val = ret * 7000 / 1024;
181		return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
182	default:
183		return -EOPNOTSUPP;
184	}
185}
186
187static int adt7411_read_in_chan(struct device *dev, u32 attr, int channel,
188				long *val)
189{
190	struct adt7411_data *data = dev_get_drvdata(dev);
191	struct i2c_client *client = data->client;
 
192
193	int ret;
194	int lsb_reg, lsb_shift;
195	int nr = channel - 1;
196
197	mutex_lock(&data->update_lock);
198	if (time_after_eq(jiffies, data->next_update)) {
199		ret = i2c_smbus_read_byte_data(client, ADT7411_REG_CFG3);
200		if (ret < 0)
201			goto exit_unlock;
202
203		if (ret & ADT7411_CFG3_REF_VDD) {
204			ret = adt7411_read_in_vdd(dev, hwmon_in_input,
205						  &data->vref_cached);
206			if (ret < 0)
207				goto exit_unlock;
208		} else {
209			data->vref_cached = 2250;
210		}
211
212		data->next_update = jiffies + HZ;
213	}
214
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
215	switch (attr) {
216	case hwmon_in_input:
217		lsb_reg = ADT7411_REG_EXT_TEMP_AIN14_LSB + (nr >> 2);
218		lsb_shift = 2 * (nr & 0x03);
219		ret = adt7411_read_10_bit(client, lsb_reg,
220					  ADT7411_REG_EXT_TEMP_AIN1_MSB + nr,
221					  lsb_shift);
222		if (ret < 0)
223			goto exit_unlock;
224		*val = ret * data->vref_cached / 1024;
225		ret = 0;
226		break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
227	default:
228		ret = -EOPNOTSUPP;
229		break;
230	}
231 exit_unlock:
232	mutex_unlock(&data->update_lock);
233	return ret;
234}
235
236static int adt7411_read_in(struct device *dev, u32 attr, int channel,
237			   long *val)
238{
239	if (channel == 0)
240		return adt7411_read_in_vdd(dev, attr, val);
241	else
242		return adt7411_read_in_chan(dev, attr, channel, val);
243}
244
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
245static int adt7411_read_temp(struct device *dev, u32 attr, int channel,
246			     long *val)
247{
248	struct adt7411_data *data = dev_get_drvdata(dev);
249	struct i2c_client *client = data->client;
250	int ret, regl, regh;
251
252	switch (attr) {
253	case hwmon_temp_input:
254		regl = channel ? ADT7411_REG_EXT_TEMP_AIN14_LSB :
255				 ADT7411_REG_INT_TEMP_VDD_LSB;
256		regh = channel ? ADT7411_REG_EXT_TEMP_AIN1_MSB :
257				 ADT7411_REG_INT_TEMP_MSB;
258		ret = adt7411_read_10_bit(client, regl, regh, 0);
259		if (ret < 0)
260			return ret;
261		ret = ret & 0x200 ? ret - 0x400 : ret; /* 10 bit signed */
262		*val = ret * 250;
263		return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
264	default:
265		return -EOPNOTSUPP;
266	}
267}
268
269static int adt7411_read(struct device *dev, enum hwmon_sensor_types type,
270			u32 attr, int channel, long *val)
271{
272	switch (type) {
273	case hwmon_in:
274		return adt7411_read_in(dev, attr, channel, val);
275	case hwmon_temp:
276		return adt7411_read_temp(dev, attr, channel, val);
277	default:
278		return -EOPNOTSUPP;
279	}
280}
281
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
282static umode_t adt7411_is_visible(const void *_data,
283				  enum hwmon_sensor_types type,
284				  u32 attr, int channel)
285{
286	const struct adt7411_data *data = _data;
 
287
288	switch (type) {
289	case hwmon_in:
290		if (channel > 0 && channel < 3)
291			return data->use_ext_temp ? 0 : S_IRUGO;
292		else
293			return S_IRUGO;
 
 
 
 
 
 
294	case hwmon_temp:
295		if (channel == 1)
296			return data->use_ext_temp ? S_IRUGO : 0;
297		else
298			return S_IRUGO;
 
 
 
 
 
 
 
 
299	default:
300		return 0;
301	}
 
302}
303
304static int adt7411_detect(struct i2c_client *client,
305			  struct i2c_board_info *info)
306{
307	int val;
308
309	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
310		return -ENODEV;
311
312	val = i2c_smbus_read_byte_data(client, ADT7411_REG_MANUFACTURER_ID);
313	if (val < 0 || val != ADT7411_MANUFACTURER_ID) {
314		dev_dbg(&client->dev,
315			"Wrong manufacturer ID. Got %d, expected %d\n",
316			val, ADT7411_MANUFACTURER_ID);
317		return -ENODEV;
318	}
319
320	val = i2c_smbus_read_byte_data(client, ADT7411_REG_DEVICE_ID);
321	if (val < 0 || val != ADT7411_DEVICE_ID) {
322		dev_dbg(&client->dev,
323			"Wrong device ID. Got %d, expected %d\n",
324			val, ADT7411_DEVICE_ID);
325		return -ENODEV;
326	}
327
328	strlcpy(info->type, "adt7411", I2C_NAME_SIZE);
329
330	return 0;
331}
332
333static int adt7411_init_device(struct adt7411_data *data)
334{
335	int ret;
336	u8 val;
337
338	ret = i2c_smbus_read_byte_data(data->client, ADT7411_REG_CFG3);
339	if (ret < 0)
340		return ret;
341
342	/*
343	 * We must only write zero to bit 1 and bit 2 and only one to bit 3
344	 * according to the datasheet.
345	 */
346	val = ret;
347	val &= ~(ADT7411_CFG3_RESERVED_BIT1 | ADT7411_CFG3_RESERVED_BIT2);
348	val |= ADT7411_CFG3_RESERVED_BIT3;
349
350	ret = i2c_smbus_write_byte_data(data->client, ADT7411_REG_CFG3, val);
351	if (ret < 0)
352		return ret;
353
354	ret = i2c_smbus_read_byte_data(data->client, ADT7411_REG_CFG1);
355	if (ret < 0)
356		return ret;
357
358	data->use_ext_temp = ret & ADT7411_CFG1_EXT_TDM;
359
360	/*
361	 * We must only write zero to bit 1 and only one to bit 3 according to
362	 * the datasheet.
363	 */
364	val = ret;
365	val &= ~ADT7411_CFG1_RESERVED_BIT1;
366	val |= ADT7411_CFG1_RESERVED_BIT3;
367
368	/* enable monitoring */
369	val |= ADT7411_CFG1_START_MONITOR;
370
371	return i2c_smbus_write_byte_data(data->client, ADT7411_REG_CFG1, val);
372}
373
374static const u32 adt7411_in_config[] = {
375	HWMON_I_INPUT,
376	HWMON_I_INPUT,
377	HWMON_I_INPUT,
378	HWMON_I_INPUT,
379	HWMON_I_INPUT,
380	HWMON_I_INPUT,
381	HWMON_I_INPUT,
382	HWMON_I_INPUT,
383	HWMON_I_INPUT,
384	0
385};
386
387static const struct hwmon_channel_info adt7411_in = {
388	.type = hwmon_in,
389	.config = adt7411_in_config,
390};
391
392static const u32 adt7411_temp_config[] = {
393	HWMON_T_INPUT,
394	HWMON_T_INPUT,
 
 
395	0
396};
397
398static const struct hwmon_channel_info adt7411_temp = {
399	.type = hwmon_temp,
400	.config = adt7411_temp_config,
401};
402
403static const struct hwmon_channel_info *adt7411_info[] = {
404	&adt7411_in,
405	&adt7411_temp,
406	NULL
407};
408
409static const struct hwmon_ops adt7411_hwmon_ops = {
410	.is_visible = adt7411_is_visible,
411	.read = adt7411_read,
 
412};
413
414static const struct hwmon_chip_info adt7411_chip_info = {
415	.ops = &adt7411_hwmon_ops,
416	.info = adt7411_info,
417};
418
419static int adt7411_probe(struct i2c_client *client,
420				   const struct i2c_device_id *id)
421{
422	struct device *dev = &client->dev;
423	struct adt7411_data *data;
424	struct device *hwmon_dev;
425	int ret;
426
427	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
428	if (!data)
429		return -ENOMEM;
430
431	i2c_set_clientdata(client, data);
432	data->client = client;
433	mutex_init(&data->device_lock);
434	mutex_init(&data->update_lock);
435
436	ret = adt7411_init_device(data);
437	if (ret < 0)
438		return ret;
439
440	/* force update on first occasion */
441	data->next_update = jiffies;
442
443	hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
444							 data,
445							 &adt7411_chip_info,
446							 adt7411_groups);
447	return PTR_ERR_OR_ZERO(hwmon_dev);
448}
449
450static const struct i2c_device_id adt7411_id[] = {
451	{ "adt7411", 0 },
452	{ }
453};
454MODULE_DEVICE_TABLE(i2c, adt7411_id);
455
456static struct i2c_driver adt7411_driver = {
457	.driver		= {
458		.name		= "adt7411",
459	},
460	.probe  = adt7411_probe,
461	.id_table = adt7411_id,
462	.detect = adt7411_detect,
463	.address_list = normal_i2c,
464	.class = I2C_CLASS_HWMON,
465};
466
467module_i2c_driver(adt7411_driver);
468
469MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de> and "
470	"Wolfram Sang <w.sang@pengutronix.de>");
471MODULE_DESCRIPTION("ADT7411 driver");
472MODULE_LICENSE("GPL v2");
v4.17
  1/*
  2 *  Driver for the ADT7411 (I2C/SPI 8 channel 10 bit ADC & temperature-sensor)
  3 *
  4 *  Copyright (C) 2008, 2010 Pengutronix
  5 *
  6 *  This program is free software; you can redistribute it and/or modify
  7 *  it under the terms of the GNU General Public License version 2 as
  8 *  published by the Free Software Foundation.
  9 *
 10 *  TODO: SPI, use power-down mode for suspend?, interrupt handling?
 11 */
 12
 13#include <linux/kernel.h>
 14#include <linux/module.h>
 15#include <linux/init.h>
 16#include <linux/err.h>
 17#include <linux/mutex.h>
 18#include <linux/jiffies.h>
 19#include <linux/i2c.h>
 20#include <linux/hwmon.h>
 21#include <linux/hwmon-sysfs.h>
 22#include <linux/slab.h>
 23
 24#define ADT7411_REG_STAT_1			0x00
 25#define ADT7411_STAT_1_INT_TEMP_HIGH		BIT(0)
 26#define ADT7411_STAT_1_INT_TEMP_LOW		BIT(1)
 27#define ADT7411_STAT_1_EXT_TEMP_HIGH_AIN1	BIT(2)
 28#define ADT7411_STAT_1_EXT_TEMP_LOW		BIT(3)
 29#define ADT7411_STAT_1_EXT_TEMP_FAULT		BIT(4)
 30#define ADT7411_STAT_1_AIN2			BIT(5)
 31#define ADT7411_STAT_1_AIN3			BIT(6)
 32#define ADT7411_STAT_1_AIN4			BIT(7)
 33#define ADT7411_REG_STAT_2			0x01
 34#define ADT7411_STAT_2_AIN5			BIT(0)
 35#define ADT7411_STAT_2_AIN6			BIT(1)
 36#define ADT7411_STAT_2_AIN7			BIT(2)
 37#define ADT7411_STAT_2_AIN8			BIT(3)
 38#define ADT7411_STAT_2_VDD			BIT(4)
 39#define ADT7411_REG_INT_TEMP_VDD_LSB		0x03
 40#define ADT7411_REG_EXT_TEMP_AIN14_LSB		0x04
 41#define ADT7411_REG_VDD_MSB			0x06
 42#define ADT7411_REG_INT_TEMP_MSB		0x07
 43#define ADT7411_REG_EXT_TEMP_AIN1_MSB		0x08
 44
 45#define ADT7411_REG_CFG1			0x18
 46#define ADT7411_CFG1_START_MONITOR		BIT(0)
 47#define ADT7411_CFG1_RESERVED_BIT1		BIT(1)
 48#define ADT7411_CFG1_EXT_TDM			BIT(2)
 49#define ADT7411_CFG1_RESERVED_BIT3		BIT(3)
 50
 51#define ADT7411_REG_CFG2			0x19
 52#define ADT7411_CFG2_DISABLE_AVG		BIT(5)
 53
 54#define ADT7411_REG_CFG3			0x1a
 55#define ADT7411_CFG3_ADC_CLK_225		BIT(0)
 56#define ADT7411_CFG3_RESERVED_BIT1		BIT(1)
 57#define ADT7411_CFG3_RESERVED_BIT2		BIT(2)
 58#define ADT7411_CFG3_RESERVED_BIT3		BIT(3)
 59#define ADT7411_CFG3_REF_VDD			BIT(4)
 60
 61#define ADT7411_REG_VDD_HIGH			0x23
 62#define ADT7411_REG_VDD_LOW			0x24
 63#define ADT7411_REG_TEMP_HIGH(nr)		(0x25 + 2 * (nr))
 64#define ADT7411_REG_TEMP_LOW(nr)		(0x26 + 2 * (nr))
 65#define ADT7411_REG_IN_HIGH(nr)		((nr) > 1 \
 66						  ? 0x2b + 2 * ((nr)-2) \
 67						  : 0x27)
 68#define ADT7411_REG_IN_LOW(nr)			((nr) > 1 \
 69						  ? 0x2c + 2 * ((nr)-2) \
 70						  : 0x28)
 71
 72#define ADT7411_REG_DEVICE_ID			0x4d
 73#define ADT7411_REG_MANUFACTURER_ID		0x4e
 74
 75#define ADT7411_DEVICE_ID			0x2
 76#define ADT7411_MANUFACTURER_ID			0x41
 77
 78static const unsigned short normal_i2c[] = { 0x48, 0x4a, 0x4b, I2C_CLIENT_END };
 79
 80static const u8 adt7411_in_alarm_reg[] = {
 81	ADT7411_REG_STAT_2,
 82	ADT7411_REG_STAT_1,
 83	ADT7411_REG_STAT_1,
 84	ADT7411_REG_STAT_1,
 85	ADT7411_REG_STAT_1,
 86	ADT7411_REG_STAT_2,
 87	ADT7411_REG_STAT_2,
 88	ADT7411_REG_STAT_2,
 89	ADT7411_REG_STAT_2,
 90};
 91
 92static const u8 adt7411_in_alarm_bits[] = {
 93	ADT7411_STAT_2_VDD,
 94	ADT7411_STAT_1_EXT_TEMP_HIGH_AIN1,
 95	ADT7411_STAT_1_AIN2,
 96	ADT7411_STAT_1_AIN3,
 97	ADT7411_STAT_1_AIN4,
 98	ADT7411_STAT_2_AIN5,
 99	ADT7411_STAT_2_AIN6,
100	ADT7411_STAT_2_AIN7,
101	ADT7411_STAT_2_AIN8,
102};
103
104struct adt7411_data {
105	struct mutex device_lock;	/* for "atomic" device accesses */
106	struct mutex update_lock;
107	unsigned long next_update;
108	long vref_cached;
109	struct i2c_client *client;
110	bool use_ext_temp;
111};
112
113/*
114 * When reading a register containing (up to 4) lsb, all associated
115 * msb-registers get locked by the hardware. After _one_ of those msb is read,
116 * _all_ are unlocked. In order to use this locking correctly, reading lsb/msb
117 * is protected here with a mutex, too.
118 */
119static int adt7411_read_10_bit(struct i2c_client *client, u8 lsb_reg,
120				u8 msb_reg, u8 lsb_shift)
121{
122	struct adt7411_data *data = i2c_get_clientdata(client);
123	int val, tmp;
124
125	mutex_lock(&data->device_lock);
126
127	val = i2c_smbus_read_byte_data(client, lsb_reg);
128	if (val < 0)
129		goto exit_unlock;
130
131	tmp = (val >> lsb_shift) & 3;
132	val = i2c_smbus_read_byte_data(client, msb_reg);
133
134	if (val >= 0)
135		val = (val << 2) | tmp;
136
137 exit_unlock:
138	mutex_unlock(&data->device_lock);
139
140	return val;
141}
142
143static int adt7411_modify_bit(struct i2c_client *client, u8 reg, u8 bit,
144				bool flag)
145{
146	struct adt7411_data *data = i2c_get_clientdata(client);
147	int ret, val;
148
149	mutex_lock(&data->device_lock);
150
151	ret = i2c_smbus_read_byte_data(client, reg);
152	if (ret < 0)
153		goto exit_unlock;
154
155	if (flag)
156		val = ret | bit;
157	else
158		val = ret & ~bit;
159
160	ret = i2c_smbus_write_byte_data(client, reg, val);
161
162 exit_unlock:
163	mutex_unlock(&data->device_lock);
164	return ret;
165}
166
167static ssize_t adt7411_show_bit(struct device *dev,
168				struct device_attribute *attr, char *buf)
169{
170	struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr);
171	struct adt7411_data *data = dev_get_drvdata(dev);
172	struct i2c_client *client = data->client;
173	int ret = i2c_smbus_read_byte_data(client, attr2->index);
174
175	return ret < 0 ? ret : sprintf(buf, "%u\n", !!(ret & attr2->nr));
176}
177
178static ssize_t adt7411_set_bit(struct device *dev,
179			       struct device_attribute *attr, const char *buf,
180			       size_t count)
181{
182	struct sensor_device_attribute_2 *s_attr2 = to_sensor_dev_attr_2(attr);
183	struct adt7411_data *data = dev_get_drvdata(dev);
184	struct i2c_client *client = data->client;
185	int ret;
186	unsigned long flag;
187
188	ret = kstrtoul(buf, 0, &flag);
189	if (ret || flag > 1)
190		return -EINVAL;
191
192	ret = adt7411_modify_bit(client, s_attr2->index, s_attr2->nr, flag);
193
194	/* force update */
195	mutex_lock(&data->update_lock);
196	data->next_update = jiffies;
197	mutex_unlock(&data->update_lock);
198
199	return ret < 0 ? ret : count;
200}
201
202#define ADT7411_BIT_ATTR(__name, __reg, __bit) \
203	SENSOR_DEVICE_ATTR_2(__name, S_IRUGO | S_IWUSR, adt7411_show_bit, \
204	adt7411_set_bit, __bit, __reg)
205
206static ADT7411_BIT_ATTR(no_average, ADT7411_REG_CFG2, ADT7411_CFG2_DISABLE_AVG);
207static ADT7411_BIT_ATTR(fast_sampling, ADT7411_REG_CFG3, ADT7411_CFG3_ADC_CLK_225);
208static ADT7411_BIT_ATTR(adc_ref_vdd, ADT7411_REG_CFG3, ADT7411_CFG3_REF_VDD);
209
210static struct attribute *adt7411_attrs[] = {
211	&sensor_dev_attr_no_average.dev_attr.attr,
212	&sensor_dev_attr_fast_sampling.dev_attr.attr,
213	&sensor_dev_attr_adc_ref_vdd.dev_attr.attr,
214	NULL
215};
216ATTRIBUTE_GROUPS(adt7411);
217
218static int adt7411_read_in_alarm(struct device *dev, int channel, long *val)
219{
220	struct adt7411_data *data = dev_get_drvdata(dev);
221	struct i2c_client *client = data->client;
222	int ret;
223
224	ret = i2c_smbus_read_byte_data(client, adt7411_in_alarm_reg[channel]);
225	if (ret < 0)
226		return ret;
227	*val = !!(ret & adt7411_in_alarm_bits[channel]);
228	return 0;
229}
230
231static int adt7411_read_in_vdd(struct device *dev, u32 attr, long *val)
232{
233	struct adt7411_data *data = dev_get_drvdata(dev);
234	struct i2c_client *client = data->client;
235	int ret;
236
237	switch (attr) {
238	case hwmon_in_input:
239		ret = adt7411_read_10_bit(client, ADT7411_REG_INT_TEMP_VDD_LSB,
240					  ADT7411_REG_VDD_MSB, 2);
241		if (ret < 0)
242			return ret;
243		*val = ret * 7000 / 1024;
244		return 0;
245	case hwmon_in_min:
246		ret = i2c_smbus_read_byte_data(client, ADT7411_REG_VDD_LOW);
247		if (ret < 0)
248			return ret;
249		*val = ret * 7000 / 256;
250		return 0;
251	case hwmon_in_max:
252		ret = i2c_smbus_read_byte_data(client, ADT7411_REG_VDD_HIGH);
253		if (ret < 0)
254			return ret;
255		*val = ret * 7000 / 256;
256		return 0;
257	case hwmon_in_alarm:
258		return adt7411_read_in_alarm(dev, 0, val);
259	default:
260		return -EOPNOTSUPP;
261	}
262}
263
264static int adt7411_update_vref(struct device *dev)
 
265{
266	struct adt7411_data *data = dev_get_drvdata(dev);
267	struct i2c_client *client = data->client;
268	int val;
269
 
 
 
 
 
270	if (time_after_eq(jiffies, data->next_update)) {
271		val = i2c_smbus_read_byte_data(client, ADT7411_REG_CFG3);
272		if (val < 0)
273			return val;
274
275		if (val & ADT7411_CFG3_REF_VDD) {
276			val = adt7411_read_in_vdd(dev, hwmon_in_input,
277						  &data->vref_cached);
278			if (val < 0)
279				return val;
280		} else {
281			data->vref_cached = 2250;
282		}
283
284		data->next_update = jiffies + HZ;
285	}
286
287	return 0;
288}
289
290static int adt7411_read_in_chan(struct device *dev, u32 attr, int channel,
291				long *val)
292{
293	struct adt7411_data *data = dev_get_drvdata(dev);
294	struct i2c_client *client = data->client;
295
296	int ret;
297	int reg, lsb_reg, lsb_shift;
298	int nr = channel - 1;
299
300	mutex_lock(&data->update_lock);
301	ret = adt7411_update_vref(dev);
302	if (ret < 0)
303		goto exit_unlock;
304
305	switch (attr) {
306	case hwmon_in_input:
307		lsb_reg = ADT7411_REG_EXT_TEMP_AIN14_LSB + (nr >> 2);
308		lsb_shift = 2 * (nr & 0x03);
309		ret = adt7411_read_10_bit(client, lsb_reg,
310					  ADT7411_REG_EXT_TEMP_AIN1_MSB + nr,
311					  lsb_shift);
312		if (ret < 0)
313			goto exit_unlock;
314		*val = ret * data->vref_cached / 1024;
315		ret = 0;
316		break;
317	case hwmon_in_min:
318	case hwmon_in_max:
319		reg = (attr == hwmon_in_min)
320			? ADT7411_REG_IN_LOW(channel)
321			: ADT7411_REG_IN_HIGH(channel);
322		ret = i2c_smbus_read_byte_data(client, reg);
323		if (ret < 0)
324			goto exit_unlock;
325		*val = ret * data->vref_cached / 256;
326		ret = 0;
327		break;
328	case hwmon_in_alarm:
329		ret = adt7411_read_in_alarm(dev, channel, val);
330		break;
331	default:
332		ret = -EOPNOTSUPP;
333		break;
334	}
335 exit_unlock:
336	mutex_unlock(&data->update_lock);
337	return ret;
338}
339
340static int adt7411_read_in(struct device *dev, u32 attr, int channel,
341			   long *val)
342{
343	if (channel == 0)
344		return adt7411_read_in_vdd(dev, attr, val);
345	else
346		return adt7411_read_in_chan(dev, attr, channel, val);
347}
348
349
350static int adt7411_read_temp_alarm(struct device *dev, u32 attr, int channel,
351				   long *val)
352{
353	struct adt7411_data *data = dev_get_drvdata(dev);
354	struct i2c_client *client = data->client;
355	int ret, bit;
356
357	ret = i2c_smbus_read_byte_data(client, ADT7411_REG_STAT_1);
358	if (ret < 0)
359		return ret;
360
361	switch (attr) {
362	case hwmon_temp_min_alarm:
363		bit = channel ? ADT7411_STAT_1_EXT_TEMP_LOW
364			      : ADT7411_STAT_1_INT_TEMP_LOW;
365		break;
366	case hwmon_temp_max_alarm:
367		bit = channel ? ADT7411_STAT_1_EXT_TEMP_HIGH_AIN1
368			      : ADT7411_STAT_1_INT_TEMP_HIGH;
369		break;
370	case hwmon_temp_fault:
371		bit = ADT7411_STAT_1_EXT_TEMP_FAULT;
372		break;
373	default:
374		return -EOPNOTSUPP;
375	}
376
377	*val = !!(ret & bit);
378	return 0;
379}
380
381static int adt7411_read_temp(struct device *dev, u32 attr, int channel,
382			     long *val)
383{
384	struct adt7411_data *data = dev_get_drvdata(dev);
385	struct i2c_client *client = data->client;
386	int ret, reg, regl, regh;
387
388	switch (attr) {
389	case hwmon_temp_input:
390		regl = channel ? ADT7411_REG_EXT_TEMP_AIN14_LSB :
391				 ADT7411_REG_INT_TEMP_VDD_LSB;
392		regh = channel ? ADT7411_REG_EXT_TEMP_AIN1_MSB :
393				 ADT7411_REG_INT_TEMP_MSB;
394		ret = adt7411_read_10_bit(client, regl, regh, 0);
395		if (ret < 0)
396			return ret;
397		ret = ret & 0x200 ? ret - 0x400 : ret; /* 10 bit signed */
398		*val = ret * 250;
399		return 0;
400	case hwmon_temp_min:
401	case hwmon_temp_max:
402		reg = (attr == hwmon_temp_min)
403			? ADT7411_REG_TEMP_LOW(channel)
404			: ADT7411_REG_TEMP_HIGH(channel);
405		ret = i2c_smbus_read_byte_data(client, reg);
406		if (ret < 0)
407			return ret;
408		ret = ret & 0x80 ? ret - 0x100 : ret; /* 8 bit signed */
409		*val = ret * 1000;
410		return 0;
411	case hwmon_temp_min_alarm:
412	case hwmon_temp_max_alarm:
413	case hwmon_temp_fault:
414		return adt7411_read_temp_alarm(dev, attr, channel, val);
415	default:
416		return -EOPNOTSUPP;
417	}
418}
419
420static int adt7411_read(struct device *dev, enum hwmon_sensor_types type,
421			u32 attr, int channel, long *val)
422{
423	switch (type) {
424	case hwmon_in:
425		return adt7411_read_in(dev, attr, channel, val);
426	case hwmon_temp:
427		return adt7411_read_temp(dev, attr, channel, val);
428	default:
429		return -EOPNOTSUPP;
430	}
431}
432
433static int adt7411_write_in_vdd(struct device *dev, u32 attr, long val)
434{
435	struct adt7411_data *data = dev_get_drvdata(dev);
436	struct i2c_client *client = data->client;
437	int reg;
438
439	val = clamp_val(val, 0, 255 * 7000 / 256);
440	val = DIV_ROUND_CLOSEST(val * 256, 7000);
441
442	switch (attr) {
443	case hwmon_in_min:
444		reg = ADT7411_REG_VDD_LOW;
445		break;
446	case hwmon_in_max:
447		reg = ADT7411_REG_VDD_HIGH;
448		break;
449	default:
450		return -EOPNOTSUPP;
451	}
452
453	return i2c_smbus_write_byte_data(client, reg, val);
454}
455
456static int adt7411_write_in_chan(struct device *dev, u32 attr, int channel,
457				 long val)
458{
459	struct adt7411_data *data = dev_get_drvdata(dev);
460	struct i2c_client *client = data->client;
461	int ret, reg;
462
463	mutex_lock(&data->update_lock);
464	ret = adt7411_update_vref(dev);
465	if (ret < 0)
466		goto exit_unlock;
467	val = clamp_val(val, 0, 255 * data->vref_cached / 256);
468	val = DIV_ROUND_CLOSEST(val * 256, data->vref_cached);
469
470	switch (attr) {
471	case hwmon_in_min:
472		reg = ADT7411_REG_IN_LOW(channel);
473		break;
474	case hwmon_in_max:
475		reg = ADT7411_REG_IN_HIGH(channel);
476		break;
477	default:
478		ret = -EOPNOTSUPP;
479		goto exit_unlock;
480	}
481
482	ret = i2c_smbus_write_byte_data(client, reg, val);
483 exit_unlock:
484	mutex_unlock(&data->update_lock);
485	return ret;
486}
487
488static int adt7411_write_in(struct device *dev, u32 attr, int channel,
489			    long val)
490{
491	if (channel == 0)
492		return adt7411_write_in_vdd(dev, attr, val);
493	else
494		return adt7411_write_in_chan(dev, attr, channel, val);
495}
496
497static int adt7411_write_temp(struct device *dev, u32 attr, int channel,
498			      long val)
499{
500	struct adt7411_data *data = dev_get_drvdata(dev);
501	struct i2c_client *client = data->client;
502	int reg;
503
504	val = clamp_val(val, -128000, 127000);
505	val = DIV_ROUND_CLOSEST(val, 1000);
506
507	switch (attr) {
508	case hwmon_temp_min:
509		reg = ADT7411_REG_TEMP_LOW(channel);
510		break;
511	case hwmon_temp_max:
512		reg = ADT7411_REG_TEMP_HIGH(channel);
513		break;
514	default:
515		return -EOPNOTSUPP;
516	}
517
518	return i2c_smbus_write_byte_data(client, reg, val);
519}
520
521static int adt7411_write(struct device *dev, enum hwmon_sensor_types type,
522			 u32 attr, int channel, long val)
523{
524	switch (type) {
525	case hwmon_in:
526		return adt7411_write_in(dev, attr, channel, val);
527	case hwmon_temp:
528		return adt7411_write_temp(dev, attr, channel, val);
529	default:
530		return -EOPNOTSUPP;
531	}
532}
533
534static umode_t adt7411_is_visible(const void *_data,
535				  enum hwmon_sensor_types type,
536				  u32 attr, int channel)
537{
538	const struct adt7411_data *data = _data;
539	bool visible;
540
541	switch (type) {
542	case hwmon_in:
543		visible = channel == 0 || channel >= 3 || !data->use_ext_temp;
544		switch (attr) {
545		case hwmon_in_input:
546		case hwmon_in_alarm:
547			return visible ? S_IRUGO : 0;
548		case hwmon_in_min:
549		case hwmon_in_max:
550			return visible ? S_IRUGO | S_IWUSR : 0;
551		}
552		break;
553	case hwmon_temp:
554		visible = channel == 0 || data->use_ext_temp;
555		switch (attr) {
556		case hwmon_temp_input:
557		case hwmon_temp_min_alarm:
558		case hwmon_temp_max_alarm:
559		case hwmon_temp_fault:
560			return visible ? S_IRUGO : 0;
561		case hwmon_temp_min:
562		case hwmon_temp_max:
563			return visible ? S_IRUGO | S_IWUSR : 0;
564		}
565		break;
566	default:
567		break;
568	}
569	return 0;
570}
571
572static int adt7411_detect(struct i2c_client *client,
573			  struct i2c_board_info *info)
574{
575	int val;
576
577	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
578		return -ENODEV;
579
580	val = i2c_smbus_read_byte_data(client, ADT7411_REG_MANUFACTURER_ID);
581	if (val < 0 || val != ADT7411_MANUFACTURER_ID) {
582		dev_dbg(&client->dev,
583			"Wrong manufacturer ID. Got %d, expected %d\n",
584			val, ADT7411_MANUFACTURER_ID);
585		return -ENODEV;
586	}
587
588	val = i2c_smbus_read_byte_data(client, ADT7411_REG_DEVICE_ID);
589	if (val < 0 || val != ADT7411_DEVICE_ID) {
590		dev_dbg(&client->dev,
591			"Wrong device ID. Got %d, expected %d\n",
592			val, ADT7411_DEVICE_ID);
593		return -ENODEV;
594	}
595
596	strlcpy(info->type, "adt7411", I2C_NAME_SIZE);
597
598	return 0;
599}
600
601static int adt7411_init_device(struct adt7411_data *data)
602{
603	int ret;
604	u8 val;
605
606	ret = i2c_smbus_read_byte_data(data->client, ADT7411_REG_CFG3);
607	if (ret < 0)
608		return ret;
609
610	/*
611	 * We must only write zero to bit 1 and bit 2 and only one to bit 3
612	 * according to the datasheet.
613	 */
614	val = ret;
615	val &= ~(ADT7411_CFG3_RESERVED_BIT1 | ADT7411_CFG3_RESERVED_BIT2);
616	val |= ADT7411_CFG3_RESERVED_BIT3;
617
618	ret = i2c_smbus_write_byte_data(data->client, ADT7411_REG_CFG3, val);
619	if (ret < 0)
620		return ret;
621
622	ret = i2c_smbus_read_byte_data(data->client, ADT7411_REG_CFG1);
623	if (ret < 0)
624		return ret;
625
626	data->use_ext_temp = ret & ADT7411_CFG1_EXT_TDM;
627
628	/*
629	 * We must only write zero to bit 1 and only one to bit 3 according to
630	 * the datasheet.
631	 */
632	val = ret;
633	val &= ~ADT7411_CFG1_RESERVED_BIT1;
634	val |= ADT7411_CFG1_RESERVED_BIT3;
635
636	/* enable monitoring */
637	val |= ADT7411_CFG1_START_MONITOR;
638
639	return i2c_smbus_write_byte_data(data->client, ADT7411_REG_CFG1, val);
640}
641
642static const u32 adt7411_in_config[] = {
643	HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
644	HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
645	HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
646	HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
647	HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
648	HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
649	HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
650	HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
651	HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
652	0
653};
654
655static const struct hwmon_channel_info adt7411_in = {
656	.type = hwmon_in,
657	.config = adt7411_in_config,
658};
659
660static const u32 adt7411_temp_config[] = {
661	HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MIN_ALARM |
662		HWMON_T_MAX | HWMON_T_MAX_ALARM,
663	HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MIN_ALARM |
664		HWMON_T_MAX | HWMON_T_MAX_ALARM | HWMON_T_FAULT,
665	0
666};
667
668static const struct hwmon_channel_info adt7411_temp = {
669	.type = hwmon_temp,
670	.config = adt7411_temp_config,
671};
672
673static const struct hwmon_channel_info *adt7411_info[] = {
674	&adt7411_in,
675	&adt7411_temp,
676	NULL
677};
678
679static const struct hwmon_ops adt7411_hwmon_ops = {
680	.is_visible = adt7411_is_visible,
681	.read = adt7411_read,
682	.write = adt7411_write,
683};
684
685static const struct hwmon_chip_info adt7411_chip_info = {
686	.ops = &adt7411_hwmon_ops,
687	.info = adt7411_info,
688};
689
690static int adt7411_probe(struct i2c_client *client,
691				   const struct i2c_device_id *id)
692{
693	struct device *dev = &client->dev;
694	struct adt7411_data *data;
695	struct device *hwmon_dev;
696	int ret;
697
698	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
699	if (!data)
700		return -ENOMEM;
701
702	i2c_set_clientdata(client, data);
703	data->client = client;
704	mutex_init(&data->device_lock);
705	mutex_init(&data->update_lock);
706
707	ret = adt7411_init_device(data);
708	if (ret < 0)
709		return ret;
710
711	/* force update on first occasion */
712	data->next_update = jiffies;
713
714	hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
715							 data,
716							 &adt7411_chip_info,
717							 adt7411_groups);
718	return PTR_ERR_OR_ZERO(hwmon_dev);
719}
720
721static const struct i2c_device_id adt7411_id[] = {
722	{ "adt7411", 0 },
723	{ }
724};
725MODULE_DEVICE_TABLE(i2c, adt7411_id);
726
727static struct i2c_driver adt7411_driver = {
728	.driver		= {
729		.name		= "adt7411",
730	},
731	.probe  = adt7411_probe,
732	.id_table = adt7411_id,
733	.detect = adt7411_detect,
734	.address_list = normal_i2c,
735	.class = I2C_CLASS_HWMON,
736};
737
738module_i2c_driver(adt7411_driver);
739
740MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de> and "
741	"Wolfram Sang <w.sang@pengutronix.de>");
742MODULE_DESCRIPTION("ADT7411 driver");
743MODULE_LICENSE("GPL v2");