2 * An hwmon driver for the Analog Devices AD7414
4 * Copyright 2006 Stefan Roese <sr at denx.de>, DENX Software Engineering
6 * Copyright (c) 2008 PIKA Technologies
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)
14 * Copyright 2006 Tower Technologies, Alessandro Zummo <a.zummo at towertech.it>
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.
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>
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
39 static u8 AD7414_REG_LIMIT[] = { AD7414_REG_T_HIGH, AD7414_REG_T_LOW };
42 struct i2c_client *client;
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)];
50 /* REG: (0.25C/bit, two's complement) << 6 */
51 static inline int ad7414_temp_from_reg(s16 reg)
54 * use integer division instead of equivalent right shift to
55 * guarantee arithmetic shift and preserve the sign
57 return ((int)reg / 64) * 250;
60 static inline int ad7414_read(struct i2c_client *client, u8 reg)
62 if (reg == AD7414_REG_TEMP)
63 return i2c_smbus_read_word_swapped(client, reg);
65 return i2c_smbus_read_byte_data(client, reg);
68 static inline int ad7414_write(struct i2c_client *client, u8 reg, u8 value)
70 return i2c_smbus_write_byte_data(client, reg, value);
73 static struct ad7414_data *ad7414_update_device(struct device *dev)
75 struct ad7414_data *data = dev_get_drvdata(dev);
76 struct i2c_client *client = data->client;
78 mutex_lock(&data->lock);
80 if (time_after(jiffies, data->next_update) || !data->valid) {
83 dev_dbg(&client->dev, "starting ad7414 update\n");
85 value = ad7414_read(client, AD7414_REG_TEMP);
87 dev_dbg(&client->dev, "AD7414_REG_TEMP err %d\n",
90 data->temp_input = value;
92 for (i = 0; i < ARRAY_SIZE(AD7414_REG_LIMIT); ++i) {
93 value = ad7414_read(client, AD7414_REG_LIMIT[i]);
95 dev_dbg(&client->dev, "AD7414 reg %d err %d\n",
96 AD7414_REG_LIMIT[i], value);
98 data->temps[i] = value;
101 data->next_update = jiffies + HZ + HZ / 2;
105 mutex_unlock(&data->lock);
110 static ssize_t temp_input_show(struct device *dev,
111 struct device_attribute *attr, char *buf)
113 struct ad7414_data *data = ad7414_update_device(dev);
114 return sprintf(buf, "%d\n", ad7414_temp_from_reg(data->temp_input));
116 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_input, 0);
118 static ssize_t max_min_show(struct device *dev, struct device_attribute *attr,
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);
126 static ssize_t max_min_store(struct device *dev,
127 struct device_attribute *attr, const char *buf,
130 struct ad7414_data *data = dev_get_drvdata(dev);
131 struct i2c_client *client = data->client;
132 int index = to_sensor_dev_attr(attr)->index;
133 u8 reg = AD7414_REG_LIMIT[index];
135 int ret = kstrtol(buf, 10, &temp);
140 temp = clamp_val(temp, -40000, 85000);
141 temp = (temp + (temp < 0 ? -500 : 500)) / 1000;
143 mutex_lock(&data->lock);
144 data->temps[index] = temp;
145 ad7414_write(client, reg, temp);
146 mutex_unlock(&data->lock);
150 static SENSOR_DEVICE_ATTR_RW(temp1_max, max_min, 0);
151 static SENSOR_DEVICE_ATTR_RW(temp1_min, max_min, 1);
153 static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
156 int bitnr = to_sensor_dev_attr(attr)->index;
157 struct ad7414_data *data = ad7414_update_device(dev);
158 int value = (data->temp_input >> bitnr) & 1;
159 return sprintf(buf, "%d\n", value);
162 static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, alarm, 3);
163 static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 4);
165 static struct attribute *ad7414_attrs[] = {
166 &sensor_dev_attr_temp1_input.dev_attr.attr,
167 &sensor_dev_attr_temp1_max.dev_attr.attr,
168 &sensor_dev_attr_temp1_min.dev_attr.attr,
169 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
170 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
174 ATTRIBUTE_GROUPS(ad7414);
176 static int ad7414_probe(struct i2c_client *client,
177 const struct i2c_device_id *dev_id)
179 struct device *dev = &client->dev;
180 struct ad7414_data *data;
181 struct device *hwmon_dev;
184 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA |
185 I2C_FUNC_SMBUS_READ_WORD_DATA))
188 data = devm_kzalloc(dev, sizeof(struct ad7414_data), GFP_KERNEL);
192 data->client = client;
193 mutex_init(&data->lock);
195 dev_info(&client->dev, "chip found\n");
197 /* Make sure the chip is powered up. */
198 conf = i2c_smbus_read_byte_data(client, AD7414_REG_CONF);
200 dev_warn(dev, "ad7414_probe unable to read config register.\n");
203 i2c_smbus_write_byte_data(client, AD7414_REG_CONF, conf);
206 hwmon_dev = devm_hwmon_device_register_with_groups(dev,
208 data, ad7414_groups);
209 return PTR_ERR_OR_ZERO(hwmon_dev);
212 static const struct i2c_device_id ad7414_id[] = {
216 MODULE_DEVICE_TABLE(i2c, ad7414_id);
218 static const struct of_device_id ad7414_of_match[] = {
219 { .compatible = "ad,ad7414" },
222 MODULE_DEVICE_TABLE(of, ad7414_of_match);
224 static struct i2c_driver ad7414_driver = {
227 .of_match_table = of_match_ptr(ad7414_of_match),
229 .probe = ad7414_probe,
230 .id_table = ad7414_id,
233 module_i2c_driver(ad7414_driver);
235 MODULE_AUTHOR("Stefan Roese <sr at denx.de>, "
236 "Frank Edelhaeuser <frank.edelhaeuser at spansion.com>");
238 MODULE_DESCRIPTION("AD7414 driver");
239 MODULE_LICENSE("GPL");