Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * LM73 Sensor driver
  4 * Based on LM75
  5 *
  6 * Copyright (C) 2007, CenoSYS (www.cenosys.com).
  7 * Copyright (C) 2009, Bollore telecom (www.bolloretelecom.eu).
  8 *
  9 * Guillaume Ligneul <guillaume.ligneul@gmail.com>
 10 * Adrien Demarez <adrien.demarez@bolloretelecom.eu>
 11 * Jeremy Laine <jeremy.laine@bolloretelecom.eu>
 12 * Chris Verges <kg4ysn@gmail.com>
 
 
 
 13 */
 14
 15#include <linux/module.h>
 16#include <linux/init.h>
 17#include <linux/i2c.h>
 18#include <linux/hwmon.h>
 19#include <linux/hwmon-sysfs.h>
 20#include <linux/err.h>
 21
 22
 23/* Addresses scanned */
 24static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4c,
 25					0x4d, 0x4e, I2C_CLIENT_END };
 26
 27/* LM73 registers */
 28#define LM73_REG_INPUT		0x00
 29#define LM73_REG_CONF		0x01
 30#define LM73_REG_MAX		0x02
 31#define LM73_REG_MIN		0x03
 32#define LM73_REG_CTRL		0x04
 33#define LM73_REG_ID		0x07
 34
 35#define LM73_ID			0x9001	/* 0x0190, byte-swapped */
 36#define DRVNAME			"lm73"
 37#define LM73_TEMP_MIN		(-256000 / 250)
 38#define LM73_TEMP_MAX		(255750 / 250)
 39
 40#define LM73_CTRL_RES_SHIFT	5
 41#define LM73_CTRL_RES_MASK	(BIT(5) | BIT(6))
 42#define LM73_CTRL_TO_MASK	BIT(7)
 43
 44#define LM73_CTRL_HI_SHIFT	2
 45#define LM73_CTRL_LO_SHIFT	1
 46
 47static const unsigned short lm73_convrates[] = {
 48	14,	/* 11-bits (0.25000 C/LSB): RES1 Bit = 0, RES0 Bit = 0 */
 49	28,	/* 12-bits (0.12500 C/LSB): RES1 Bit = 0, RES0 Bit = 1 */
 50	56,	/* 13-bits (0.06250 C/LSB): RES1 Bit = 1, RES0 Bit = 0 */
 51	112,	/* 14-bits (0.03125 C/LSB): RES1 Bit = 1, RES0 Bit = 1 */
 52};
 53
 54struct lm73_data {
 55	struct i2c_client *client;
 56	struct mutex lock;
 57	u8 ctrl;			/* control register value */
 58};
 59
 60/*-----------------------------------------------------------------------*/
 61
 62static ssize_t temp_store(struct device *dev, struct device_attribute *da,
 63			  const char *buf, size_t count)
 
 64{
 65	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
 66	struct lm73_data *data = dev_get_drvdata(dev);
 67	long temp;
 68	short value;
 69	s32 err;
 70
 71	int status = kstrtol(buf, 10, &temp);
 72	if (status < 0)
 73		return status;
 74
 75	/* Write value */
 76	value = clamp_val(temp / 250, LM73_TEMP_MIN, LM73_TEMP_MAX) << 5;
 77	err = i2c_smbus_write_word_swapped(data->client, attr->index, value);
 78	return (err < 0) ? err : count;
 
 79}
 80
 81static ssize_t temp_show(struct device *dev, struct device_attribute *da,
 82			 char *buf)
 83{
 84	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
 85	struct lm73_data *data = dev_get_drvdata(dev);
 86	int temp;
 87
 88	s32 err = i2c_smbus_read_word_swapped(data->client, attr->index);
 89	if (err < 0)
 90		return err;
 91
 92	/* use integer division instead of equivalent right shift to
 93	   guarantee arithmetic shift and preserve the sign */
 94	temp = (((s16) err) * 250) / 32;
 95	return sysfs_emit(buf, "%d\n", temp);
 
 96}
 97
 98static ssize_t convrate_store(struct device *dev, struct device_attribute *da,
 99			      const char *buf, size_t count)
100{
101	struct lm73_data *data = dev_get_drvdata(dev);
102	unsigned long convrate;
103	s32 err;
104	int res = 0;
105
106	err = kstrtoul(buf, 10, &convrate);
107	if (err < 0)
108		return err;
109
110	/*
111	 * Convert the desired conversion rate into register bits.
112	 * res is already initialized, and everything past the second-to-last
113	 * value in the array is treated as belonging to the last value
114	 * in the array.
115	 */
116	while (res < (ARRAY_SIZE(lm73_convrates) - 1) &&
117			convrate > lm73_convrates[res])
118		res++;
119
120	mutex_lock(&data->lock);
121	data->ctrl &= LM73_CTRL_TO_MASK;
122	data->ctrl |= res << LM73_CTRL_RES_SHIFT;
123	err = i2c_smbus_write_byte_data(data->client, LM73_REG_CTRL,
124					data->ctrl);
125	mutex_unlock(&data->lock);
126
127	if (err < 0)
128		return err;
129
130	return count;
131}
132
133static ssize_t convrate_show(struct device *dev, struct device_attribute *da,
134			     char *buf)
135{
136	struct lm73_data *data = dev_get_drvdata(dev);
137	int res;
138
139	res = (data->ctrl & LM73_CTRL_RES_MASK) >> LM73_CTRL_RES_SHIFT;
140	return sysfs_emit(buf, "%hu\n", lm73_convrates[res]);
141}
142
143static ssize_t maxmin_alarm_show(struct device *dev,
144				 struct device_attribute *da, char *buf)
145{
146	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
147	struct lm73_data *data = dev_get_drvdata(dev);
148	s32 ctrl;
149
150	mutex_lock(&data->lock);
151	ctrl = i2c_smbus_read_byte_data(data->client, LM73_REG_CTRL);
152	if (ctrl < 0)
153		goto abort;
154	data->ctrl = ctrl;
155	mutex_unlock(&data->lock);
156
157	return sysfs_emit(buf, "%d\n", (ctrl >> attr->index) & 1);
158
159abort:
160	mutex_unlock(&data->lock);
161	return ctrl;
162}
163
164/*-----------------------------------------------------------------------*/
165
166/* sysfs attributes for hwmon */
167
168static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, LM73_REG_MAX);
169static SENSOR_DEVICE_ATTR_RW(temp1_min, temp, LM73_REG_MIN);
170static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, LM73_REG_INPUT);
171static SENSOR_DEVICE_ATTR_RW(update_interval, convrate, 0);
172static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, maxmin_alarm,
173			     LM73_CTRL_HI_SHIFT);
174static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, maxmin_alarm,
175			     LM73_CTRL_LO_SHIFT);
176
177static struct attribute *lm73_attrs[] = {
178	&sensor_dev_attr_temp1_input.dev_attr.attr,
179	&sensor_dev_attr_temp1_max.dev_attr.attr,
180	&sensor_dev_attr_temp1_min.dev_attr.attr,
181	&sensor_dev_attr_update_interval.dev_attr.attr,
182	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
183	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
184	NULL
185};
186ATTRIBUTE_GROUPS(lm73);
 
 
 
187
188/*-----------------------------------------------------------------------*/
189
190/* device probe and removal */
191
192static int
193lm73_probe(struct i2c_client *client)
194{
195	struct device *dev = &client->dev;
196	struct device *hwmon_dev;
197	struct lm73_data *data;
198	int ctrl;
199
200	data = devm_kzalloc(dev, sizeof(struct lm73_data), GFP_KERNEL);
201	if (!data)
202		return -ENOMEM;
203
204	data->client = client;
205	mutex_init(&data->lock);
206
207	ctrl = i2c_smbus_read_byte_data(client, LM73_REG_CTRL);
208	if (ctrl < 0)
209		return ctrl;
210	data->ctrl = ctrl;
211
212	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
213							   data, lm73_groups);
214	if (IS_ERR(hwmon_dev))
215		return PTR_ERR(hwmon_dev);
 
 
 
 
 
216
217	dev_info(dev, "sensor '%s'\n", client->name);
 
 
218
 
 
219	return 0;
220}
221
222static const struct i2c_device_id lm73_ids[] = {
223	{ "lm73", 0 },
224	{ /* LIST END */ }
225};
226MODULE_DEVICE_TABLE(i2c, lm73_ids);
227
228/* Return 0 if detection is successful, -ENODEV otherwise */
229static int lm73_detect(struct i2c_client *new_client,
230			struct i2c_board_info *info)
231{
232	struct i2c_adapter *adapter = new_client->adapter;
233	int id, ctrl, conf;
 
234
235	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
236					I2C_FUNC_SMBUS_WORD_DATA))
237		return -ENODEV;
238
239	/*
240	 * Do as much detection as possible with byte reads first, as word
241	 * reads can confuse other devices.
242	 */
243	ctrl = i2c_smbus_read_byte_data(new_client, LM73_REG_CTRL);
244	if (ctrl < 0 || (ctrl & 0x10))
245		return -ENODEV;
246
247	conf = i2c_smbus_read_byte_data(new_client, LM73_REG_CONF);
248	if (conf < 0 || (conf & 0x0c))
249		return -ENODEV;
250
251	id = i2c_smbus_read_byte_data(new_client, LM73_REG_ID);
252	if (id < 0 || id != (LM73_ID & 0xff))
253		return -ENODEV;
254
255	/* Check device ID */
256	id = i2c_smbus_read_word_data(new_client, LM73_REG_ID);
257	if (id < 0 || id != LM73_ID)
 
258		return -ENODEV;
259
260	strscpy(info->type, "lm73", I2C_NAME_SIZE);
261
262	return 0;
263}
264
265static const struct of_device_id lm73_of_match[] = {
266	{
267		.compatible = "ti,lm73",
268	},
269	{ },
270};
271
272MODULE_DEVICE_TABLE(of, lm73_of_match);
273
274static struct i2c_driver lm73_driver = {
275	.class		= I2C_CLASS_HWMON,
276	.driver = {
277		.name	= "lm73",
278		.of_match_table = lm73_of_match,
279	},
280	.probe		= lm73_probe,
 
281	.id_table	= lm73_ids,
282	.detect		= lm73_detect,
283	.address_list	= normal_i2c,
284};
285
286module_i2c_driver(lm73_driver);
 
 
 
 
 
 
 
 
 
 
287
288MODULE_AUTHOR("Guillaume Ligneul <guillaume.ligneul@gmail.com>");
289MODULE_DESCRIPTION("LM73 driver");
290MODULE_LICENSE("GPL");
v3.1
 
  1/*
  2 * LM73 Sensor driver
  3 * Based on LM75
  4 *
  5 * Copyright (C) 2007, CenoSYS (www.cenosys.com).
  6 * Copyright (C) 2009, Bollore telecom (www.bolloretelecom.eu).
  7 *
  8 * Guillaume Ligneul <guillaume.ligneul@gmail.com>
  9 * Adrien Demarez <adrien.demarez@bolloretelecom.eu>
 10 * Jeremy Laine <jeremy.laine@bolloretelecom.eu>
 11 *
 12 * This software program is licensed subject to the GNU General Public License
 13 * (GPL).Version 2,June 1991, available at
 14 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 15 */
 16
 17#include <linux/module.h>
 18#include <linux/init.h>
 19#include <linux/i2c.h>
 20#include <linux/hwmon.h>
 21#include <linux/hwmon-sysfs.h>
 22#include <linux/err.h>
 23
 24
 25/* Addresses scanned */
 26static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4c,
 27					0x4d, 0x4e, I2C_CLIENT_END };
 28
 29/* LM73 registers */
 30#define LM73_REG_INPUT		0x00
 31#define LM73_REG_CONF		0x01
 32#define LM73_REG_MAX		0x02
 33#define LM73_REG_MIN		0x03
 34#define LM73_REG_CTRL		0x04
 35#define LM73_REG_ID		0x07
 36
 37#define LM73_ID			0x9001 /* or 0x190 after a swab16() */
 38#define DRVNAME			"lm73"
 39#define LM73_TEMP_MIN		(-40)
 40#define LM73_TEMP_MAX		150
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 41
 42/*-----------------------------------------------------------------------*/
 43
 44
 45static ssize_t set_temp(struct device *dev, struct device_attribute *da,
 46			const char *buf, size_t count)
 47{
 48	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
 49	struct i2c_client *client = to_i2c_client(dev);
 50	long temp;
 51	short value;
 
 52
 53	int status = strict_strtol(buf, 10, &temp);
 54	if (status < 0)
 55		return status;
 56
 57	/* Write value */
 58	value = (short) SENSORS_LIMIT(temp/250, (LM73_TEMP_MIN*4),
 59		(LM73_TEMP_MAX*4)) << 5;
 60	i2c_smbus_write_word_data(client, attr->index, swab16(value));
 61	return count;
 62}
 63
 64static ssize_t show_temp(struct device *dev, struct device_attribute *da,
 65			 char *buf)
 66{
 67	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
 68	struct i2c_client *client = to_i2c_client(dev);
 
 
 
 
 
 
 69	/* use integer division instead of equivalent right shift to
 70	   guarantee arithmetic shift and preserve the sign */
 71	int temp = ((s16) (swab16(i2c_smbus_read_word_data(client,
 72		attr->index)))*250) / 32;
 73	return sprintf(buf, "%d\n", temp);
 74}
 75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 76
 77/*-----------------------------------------------------------------------*/
 78
 79/* sysfs attributes for hwmon */
 80
 81static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
 82			show_temp, set_temp, LM73_REG_MAX);
 83static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
 84			show_temp, set_temp, LM73_REG_MIN);
 85static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
 86			show_temp, NULL, LM73_REG_INPUT);
 87
 
 88
 89static struct attribute *lm73_attributes[] = {
 90	&sensor_dev_attr_temp1_input.dev_attr.attr,
 91	&sensor_dev_attr_temp1_max.dev_attr.attr,
 92	&sensor_dev_attr_temp1_min.dev_attr.attr,
 93
 
 
 94	NULL
 95};
 96
 97static const struct attribute_group lm73_group = {
 98	.attrs = lm73_attributes,
 99};
100
101/*-----------------------------------------------------------------------*/
102
103/* device probe and removal */
104
105static int
106lm73_probe(struct i2c_client *client, const struct i2c_device_id *id)
107{
 
108	struct device *hwmon_dev;
109	int status;
 
110
111	/* Register sysfs hooks */
112	status = sysfs_create_group(&client->dev.kobj, &lm73_group);
113	if (status)
114		return status;
115
116	hwmon_dev = hwmon_device_register(&client->dev);
117	if (IS_ERR(hwmon_dev)) {
118		status = PTR_ERR(hwmon_dev);
119		goto exit_remove;
120	}
121	i2c_set_clientdata(client, hwmon_dev);
122
123	dev_info(&client->dev, "%s: sensor '%s'\n",
124		 dev_name(hwmon_dev), client->name);
125
126	return 0;
127
128exit_remove:
129	sysfs_remove_group(&client->dev.kobj, &lm73_group);
130	return status;
131}
132
133static int lm73_remove(struct i2c_client *client)
134{
135	struct device *hwmon_dev = i2c_get_clientdata(client);
136
137	hwmon_device_unregister(hwmon_dev);
138	sysfs_remove_group(&client->dev.kobj, &lm73_group);
139	return 0;
140}
141
142static const struct i2c_device_id lm73_ids[] = {
143	{ "lm73", 0 },
144	{ /* LIST END */ }
145};
146MODULE_DEVICE_TABLE(i2c, lm73_ids);
147
148/* Return 0 if detection is successful, -ENODEV otherwise */
149static int lm73_detect(struct i2c_client *new_client,
150			struct i2c_board_info *info)
151{
152	struct i2c_adapter *adapter = new_client->adapter;
153	u16 id;
154	u8 ctrl;
155
156	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
157					I2C_FUNC_SMBUS_WORD_DATA))
158		return -ENODEV;
159
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160	/* Check device ID */
161	id = i2c_smbus_read_word_data(new_client, LM73_REG_ID);
162	ctrl = i2c_smbus_read_byte_data(new_client, LM73_REG_CTRL);
163	if ((id != LM73_ID) || (ctrl & 0x10))
164		return -ENODEV;
165
166	strlcpy(info->type, "lm73", I2C_NAME_SIZE);
167
168	return 0;
169}
170
 
 
 
 
 
 
 
 
 
171static struct i2c_driver lm73_driver = {
172	.class		= I2C_CLASS_HWMON,
173	.driver = {
174		.name	= "lm73",
 
175	},
176	.probe		= lm73_probe,
177	.remove		= lm73_remove,
178	.id_table	= lm73_ids,
179	.detect		= lm73_detect,
180	.address_list	= normal_i2c,
181};
182
183/* module glue */
184
185static int __init sensors_lm73_init(void)
186{
187	return i2c_add_driver(&lm73_driver);
188}
189
190static void __exit sensors_lm73_exit(void)
191{
192	i2c_del_driver(&lm73_driver);
193}
194
195MODULE_AUTHOR("Guillaume Ligneul <guillaume.ligneul@gmail.com>");
196MODULE_DESCRIPTION("LM73 driver");
197MODULE_LICENSE("GPL");
198
199module_init(sensors_lm73_init);
200module_exit(sensors_lm73_exit);