Linux Audio

Check our new training course

Loading...
v3.1
 
  1/*
  2 * An hwmon driver for the Analog Devices AD7414
  3 *
  4 * Copyright 2006 Stefan Roese <sr at denx.de>, DENX Software Engineering
  5 *
  6 * Copyright (c) 2008 PIKA Technologies
  7 *   Sean MacLennan <smaclennan@pikatech.com>
  8 *
  9 * Copyright (c) 2008 Spansion Inc.
 10 *   Frank Edelhaeuser <frank.edelhaeuser at spansion.com>
 11 *   (converted to "new style" I2C driver model, removed checkpatch.pl warnings)
 12 *
 13 * Based on ad7418.c
 14 * Copyright 2006 Tower Technologies, Alessandro Zummo <a.zummo at towertech.it>
 15 *
 16 * This program is free software; you can redistribute it and/or modify
 17 * it under the terms of the GNU General Public License as published by
 18 * the Free Software Foundation; either version 2 of the License, or
 19 * (at your option) any later version.
 20 */
 21
 22#include <linux/module.h>
 23#include <linux/jiffies.h>
 24#include <linux/i2c.h>
 25#include <linux/hwmon.h>
 26#include <linux/hwmon-sysfs.h>
 27#include <linux/err.h>
 28#include <linux/mutex.h>
 29#include <linux/sysfs.h>
 30#include <linux/slab.h>
 31
 32
 33/* AD7414 registers */
 34#define AD7414_REG_TEMP		0x00
 35#define AD7414_REG_CONF		0x01
 36#define AD7414_REG_T_HIGH	0x02
 37#define AD7414_REG_T_LOW	0x03
 38
 39static u8 AD7414_REG_LIMIT[] = { AD7414_REG_T_HIGH, AD7414_REG_T_LOW };
 40
 41struct ad7414_data {
 42	struct device		*hwmon_dev;
 43	struct mutex		lock;	/* atomic read data updates */
 44	char			valid;	/* !=0 if following fields are valid */
 45	unsigned long		next_update;	/* In jiffies */
 46	s16			temp_input;	/* Register values */
 47	s8			temps[ARRAY_SIZE(AD7414_REG_LIMIT)];
 48};
 49
 50/* REG: (0.25C/bit, two's complement) << 6 */
 51static inline int ad7414_temp_from_reg(s16 reg)
 52{
 53	/* use integer division instead of equivalent right shift to
 
 54	 * guarantee arithmetic shift and preserve the sign
 55	 */
 56	return ((int)reg / 64) * 250;
 57}
 58
 59static inline int ad7414_read(struct i2c_client *client, u8 reg)
 60{
 61	if (reg == AD7414_REG_TEMP) {
 62		int value = i2c_smbus_read_word_data(client, reg);
 63		return (value < 0) ? value : swab16(value);
 64	} else
 65		return i2c_smbus_read_byte_data(client, reg);
 66}
 67
 68static inline int ad7414_write(struct i2c_client *client, u8 reg, u8 value)
 69{
 70	return i2c_smbus_write_byte_data(client, reg, value);
 71}
 72
 73static struct ad7414_data *ad7414_update_device(struct device *dev)
 74{
 75	struct i2c_client *client = to_i2c_client(dev);
 76	struct ad7414_data *data = i2c_get_clientdata(client);
 77
 78	mutex_lock(&data->lock);
 79
 80	if (time_after(jiffies, data->next_update) || !data->valid) {
 81		int value, i;
 82
 83		dev_dbg(&client->dev, "starting ad7414 update\n");
 84
 85		value = ad7414_read(client, AD7414_REG_TEMP);
 86		if (value < 0)
 87			dev_dbg(&client->dev, "AD7414_REG_TEMP err %d\n",
 88				value);
 89		else
 90			data->temp_input = value;
 91
 92		for (i = 0; i < ARRAY_SIZE(AD7414_REG_LIMIT); ++i) {
 93			value = ad7414_read(client, AD7414_REG_LIMIT[i]);
 94			if (value < 0)
 95				dev_dbg(&client->dev, "AD7414 reg %d err %d\n",
 96					AD7414_REG_LIMIT[i], value);
 97			else
 98				data->temps[i] = value;
 99		}
100
101		data->next_update = jiffies + HZ + HZ / 2;
102		data->valid = 1;
103	}
104
105	mutex_unlock(&data->lock);
106
107	return data;
108}
109
110static ssize_t show_temp_input(struct device *dev,
111			       struct device_attribute *attr, char *buf)
112{
113	struct ad7414_data *data = ad7414_update_device(dev);
114	return sprintf(buf, "%d\n", ad7414_temp_from_reg(data->temp_input));
115}
116static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL, 0);
117
118static ssize_t show_max_min(struct device *dev, struct device_attribute *attr,
119			  char *buf)
120{
121	int index = to_sensor_dev_attr(attr)->index;
122	struct ad7414_data *data = ad7414_update_device(dev);
123	return sprintf(buf, "%d\n", data->temps[index] * 1000);
124}
125
126static ssize_t set_max_min(struct device *dev,
127			   struct device_attribute *attr,
128			   const char *buf, size_t count)
129{
130	struct i2c_client *client = to_i2c_client(dev);
131	struct ad7414_data *data = i2c_get_clientdata(client);
132	int index = to_sensor_dev_attr(attr)->index;
133	u8 reg = AD7414_REG_LIMIT[index];
134	long temp = simple_strtol(buf, NULL, 10);
 
 
 
 
135
136	temp = SENSORS_LIMIT(temp, -40000, 85000);
137	temp = (temp + (temp < 0 ? -500 : 500)) / 1000;
138
139	mutex_lock(&data->lock);
140	data->temps[index] = temp;
141	ad7414_write(client, reg, temp);
142	mutex_unlock(&data->lock);
143	return count;
144}
145
146static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
147			  show_max_min, set_max_min, 0);
148static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
149			  show_max_min, set_max_min, 1);
150
151static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
152			  char *buf)
153{
154	int bitnr = to_sensor_dev_attr(attr)->index;
155	struct ad7414_data *data = ad7414_update_device(dev);
156	int value = (data->temp_input >> bitnr) & 1;
157	return sprintf(buf, "%d\n", value);
158}
159
160static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 3);
161static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 4);
162
163static struct attribute *ad7414_attributes[] = {
164	&sensor_dev_attr_temp1_input.dev_attr.attr,
165	&sensor_dev_attr_temp1_max.dev_attr.attr,
166	&sensor_dev_attr_temp1_min.dev_attr.attr,
167	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
168	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
169	NULL
170};
171
172static const struct attribute_group ad7414_group = {
173	.attrs = ad7414_attributes,
174};
175
176static int ad7414_probe(struct i2c_client *client,
177			const struct i2c_device_id *dev_id)
178{
 
179	struct ad7414_data *data;
 
180	int conf;
181	int err;
182
183	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA |
184				     I2C_FUNC_SMBUS_READ_WORD_DATA)) {
185		err = -EOPNOTSUPP;
186		goto exit;
187	}
188
189	data = kzalloc(sizeof(struct ad7414_data), GFP_KERNEL);
190	if (!data) {
191		err = -ENOMEM;
192		goto exit;
193	}
194
195	i2c_set_clientdata(client, data);
196	mutex_init(&data->lock);
197
198	dev_info(&client->dev, "chip found\n");
199
200	/* Make sure the chip is powered up. */
201	conf = i2c_smbus_read_byte_data(client, AD7414_REG_CONF);
202	if (conf < 0)
203		dev_warn(&client->dev,
204			 "ad7414_probe unable to read config register.\n");
205	else {
206		conf &= ~(1 << 7);
207		i2c_smbus_write_byte_data(client, AD7414_REG_CONF, conf);
208	}
209
210	/* Register sysfs hooks */
211	err = sysfs_create_group(&client->dev.kobj, &ad7414_group);
212	if (err)
213		goto exit_free;
214
215	data->hwmon_dev = hwmon_device_register(&client->dev);
216	if (IS_ERR(data->hwmon_dev)) {
217		err = PTR_ERR(data->hwmon_dev);
218		goto exit_remove;
219	}
220
221	return 0;
222
223exit_remove:
224	sysfs_remove_group(&client->dev.kobj, &ad7414_group);
225exit_free:
226	kfree(data);
227exit:
228	return err;
229}
230
231static int __devexit ad7414_remove(struct i2c_client *client)
232{
233	struct ad7414_data *data = i2c_get_clientdata(client);
234
235	hwmon_device_unregister(data->hwmon_dev);
236	sysfs_remove_group(&client->dev.kobj, &ad7414_group);
237	kfree(data);
238	return 0;
239}
240
241static const struct i2c_device_id ad7414_id[] = {
242	{ "ad7414", 0 },
243	{}
244};
245MODULE_DEVICE_TABLE(i2c, ad7414_id);
246
 
 
 
 
 
 
247static struct i2c_driver ad7414_driver = {
248	.driver = {
249		.name	= "ad7414",
 
250	},
251	.probe	= ad7414_probe,
252	.remove	= __devexit_p(ad7414_remove),
253	.id_table = ad7414_id,
254};
255
256static int __init ad7414_init(void)
257{
258	return i2c_add_driver(&ad7414_driver);
259}
260module_init(ad7414_init);
261
262static void __exit ad7414_exit(void)
263{
264	i2c_del_driver(&ad7414_driver);
265}
266module_exit(ad7414_exit);
267
268MODULE_AUTHOR("Stefan Roese <sr at denx.de>, "
269	      "Frank Edelhaeuser <frank.edelhaeuser at spansion.com>");
270
271MODULE_DESCRIPTION("AD7414 driver");
272MODULE_LICENSE("GPL");
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * An hwmon driver for the Analog Devices AD7414
  4 *
  5 * Copyright 2006 Stefan Roese <sr at denx.de>, DENX Software Engineering
  6 *
  7 * Copyright (c) 2008 PIKA Technologies
  8 *   Sean MacLennan <smaclennan@pikatech.com>
  9 *
 10 * Copyright (c) 2008 Spansion Inc.
 11 *   Frank Edelhaeuser <frank.edelhaeuser at spansion.com>
 12 *   (converted to "new style" I2C driver model, removed checkpatch.pl warnings)
 13 *
 14 * Based on ad7418.c
 15 * Copyright 2006 Tower Technologies, Alessandro Zummo <a.zummo at towertech.it>
 
 
 
 
 
 16 */
 17
 18#include <linux/module.h>
 19#include <linux/jiffies.h>
 20#include <linux/i2c.h>
 21#include <linux/hwmon.h>
 22#include <linux/hwmon-sysfs.h>
 23#include <linux/err.h>
 24#include <linux/mutex.h>
 25#include <linux/sysfs.h>
 26#include <linux/slab.h>
 27
 28
 29/* AD7414 registers */
 30#define AD7414_REG_TEMP		0x00
 31#define AD7414_REG_CONF		0x01
 32#define AD7414_REG_T_HIGH	0x02
 33#define AD7414_REG_T_LOW	0x03
 34
 35static u8 AD7414_REG_LIMIT[] = { AD7414_REG_T_HIGH, AD7414_REG_T_LOW };
 36
 37struct ad7414_data {
 38	struct i2c_client	*client;
 39	struct mutex		lock;	/* atomic read data updates */
 40	bool			valid;	/* true if following fields are valid */
 41	unsigned long		next_update;	/* In jiffies */
 42	s16			temp_input;	/* Register values */
 43	s8			temps[ARRAY_SIZE(AD7414_REG_LIMIT)];
 44};
 45
 46/* REG: (0.25C/bit, two's complement) << 6 */
 47static inline int ad7414_temp_from_reg(s16 reg)
 48{
 49	/*
 50	 * use integer division instead of equivalent right shift to
 51	 * guarantee arithmetic shift and preserve the sign
 52	 */
 53	return ((int)reg / 64) * 250;
 54}
 55
 56static inline int ad7414_read(struct i2c_client *client, u8 reg)
 57{
 58	if (reg == AD7414_REG_TEMP)
 59		return i2c_smbus_read_word_swapped(client, reg);
 60	else
 
 61		return i2c_smbus_read_byte_data(client, reg);
 62}
 63
 64static inline int ad7414_write(struct i2c_client *client, u8 reg, u8 value)
 65{
 66	return i2c_smbus_write_byte_data(client, reg, value);
 67}
 68
 69static struct ad7414_data *ad7414_update_device(struct device *dev)
 70{
 71	struct ad7414_data *data = dev_get_drvdata(dev);
 72	struct i2c_client *client = data->client;
 73
 74	mutex_lock(&data->lock);
 75
 76	if (time_after(jiffies, data->next_update) || !data->valid) {
 77		int value, i;
 78
 79		dev_dbg(&client->dev, "starting ad7414 update\n");
 80
 81		value = ad7414_read(client, AD7414_REG_TEMP);
 82		if (value < 0)
 83			dev_dbg(&client->dev, "AD7414_REG_TEMP err %d\n",
 84				value);
 85		else
 86			data->temp_input = value;
 87
 88		for (i = 0; i < ARRAY_SIZE(AD7414_REG_LIMIT); ++i) {
 89			value = ad7414_read(client, AD7414_REG_LIMIT[i]);
 90			if (value < 0)
 91				dev_dbg(&client->dev, "AD7414 reg %d err %d\n",
 92					AD7414_REG_LIMIT[i], value);
 93			else
 94				data->temps[i] = value;
 95		}
 96
 97		data->next_update = jiffies + HZ + HZ / 2;
 98		data->valid = true;
 99	}
100
101	mutex_unlock(&data->lock);
102
103	return data;
104}
105
106static ssize_t temp_input_show(struct device *dev,
107			       struct device_attribute *attr, char *buf)
108{
109	struct ad7414_data *data = ad7414_update_device(dev);
110	return sprintf(buf, "%d\n", ad7414_temp_from_reg(data->temp_input));
111}
112static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_input, 0);
113
114static ssize_t max_min_show(struct device *dev, struct device_attribute *attr,
115			    char *buf)
116{
117	int index = to_sensor_dev_attr(attr)->index;
118	struct ad7414_data *data = ad7414_update_device(dev);
119	return sprintf(buf, "%d\n", data->temps[index] * 1000);
120}
121
122static ssize_t max_min_store(struct device *dev,
123			     struct device_attribute *attr, const char *buf,
124			     size_t count)
125{
126	struct ad7414_data *data = dev_get_drvdata(dev);
127	struct i2c_client *client = data->client;
128	int index = to_sensor_dev_attr(attr)->index;
129	u8 reg = AD7414_REG_LIMIT[index];
130	long temp;
131	int ret = kstrtol(buf, 10, &temp);
132
133	if (ret < 0)
134		return ret;
135
136	temp = clamp_val(temp, -40000, 85000);
137	temp = (temp + (temp < 0 ? -500 : 500)) / 1000;
138
139	mutex_lock(&data->lock);
140	data->temps[index] = temp;
141	ad7414_write(client, reg, temp);
142	mutex_unlock(&data->lock);
143	return count;
144}
145
146static SENSOR_DEVICE_ATTR_RW(temp1_max, max_min, 0);
147static SENSOR_DEVICE_ATTR_RW(temp1_min, max_min, 1);
 
 
148
149static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
150			  char *buf)
151{
152	int bitnr = to_sensor_dev_attr(attr)->index;
153	struct ad7414_data *data = ad7414_update_device(dev);
154	int value = (data->temp_input >> bitnr) & 1;
155	return sprintf(buf, "%d\n", value);
156}
157
158static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, alarm, 3);
159static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 4);
160
161static struct attribute *ad7414_attrs[] = {
162	&sensor_dev_attr_temp1_input.dev_attr.attr,
163	&sensor_dev_attr_temp1_max.dev_attr.attr,
164	&sensor_dev_attr_temp1_min.dev_attr.attr,
165	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
166	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
167	NULL
168};
169
170ATTRIBUTE_GROUPS(ad7414);
 
 
171
172static int ad7414_probe(struct i2c_client *client)
 
173{
174	struct device *dev = &client->dev;
175	struct ad7414_data *data;
176	struct device *hwmon_dev;
177	int conf;
 
178
179	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA |
180				     I2C_FUNC_SMBUS_READ_WORD_DATA))
181		return -EOPNOTSUPP;
 
 
182
183	data = devm_kzalloc(dev, sizeof(struct ad7414_data), GFP_KERNEL);
184	if (!data)
185		return -ENOMEM;
 
 
186
187	data->client = client;
188	mutex_init(&data->lock);
189
190	dev_info(&client->dev, "chip found\n");
191
192	/* Make sure the chip is powered up. */
193	conf = i2c_smbus_read_byte_data(client, AD7414_REG_CONF);
194	if (conf < 0)
195		dev_warn(dev, "ad7414_probe unable to read config register.\n");
 
196	else {
197		conf &= ~(1 << 7);
198		i2c_smbus_write_byte_data(client, AD7414_REG_CONF, conf);
199	}
200
201	hwmon_dev = devm_hwmon_device_register_with_groups(dev,
202							   client->name,
203							   data, ad7414_groups);
204	return PTR_ERR_OR_ZERO(hwmon_dev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
205}
206
207static const struct i2c_device_id ad7414_id[] = {
208	{ "ad7414" },
209	{}
210};
211MODULE_DEVICE_TABLE(i2c, ad7414_id);
212
213static const struct of_device_id __maybe_unused ad7414_of_match[] = {
214	{ .compatible = "ad,ad7414" },
215	{ },
216};
217MODULE_DEVICE_TABLE(of, ad7414_of_match);
218
219static struct i2c_driver ad7414_driver = {
220	.driver = {
221		.name	= "ad7414",
222		.of_match_table = of_match_ptr(ad7414_of_match),
223	},
224	.probe = ad7414_probe,
 
225	.id_table = ad7414_id,
226};
227
228module_i2c_driver(ad7414_driver);
 
 
 
 
 
 
 
 
 
 
229
230MODULE_AUTHOR("Stefan Roese <sr at denx.de>, "
231	      "Frank Edelhaeuser <frank.edelhaeuser at spansion.com>");
232
233MODULE_DESCRIPTION("AD7414 driver");
234MODULE_LICENSE("GPL");