Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 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 | |
| 39 | static u8 AD7414_REG_LIMIT[] = { AD7414_REG_T_HIGH, AD7414_REG_T_LOW }; |
| 40 | |
| 41 | struct ad7414_data { |
Axel Lin | 045c139 | 2014-07-01 22:31:44 +0800 | [diff] [blame] | 42 | struct i2c_client *client; |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 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 */ |
| 51 | static inline int ad7414_temp_from_reg(s16 reg) |
| 52 | { |
Guenter Roeck | 8deeac8 | 2012-01-19 11:02:13 -0800 | [diff] [blame] | 53 | /* |
| 54 | * use integer division instead of equivalent right shift to |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 55 | * guarantee arithmetic shift and preserve the sign |
| 56 | */ |
| 57 | return ((int)reg / 64) * 250; |
| 58 | } |
| 59 | |
| 60 | static inline int ad7414_read(struct i2c_client *client, u8 reg) |
| 61 | { |
Jean Delvare | 90f4102 | 2011-11-04 12:00:47 +0100 | [diff] [blame] | 62 | if (reg == AD7414_REG_TEMP) |
| 63 | return i2c_smbus_read_word_swapped(client, reg); |
| 64 | else |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 65 | return i2c_smbus_read_byte_data(client, reg); |
| 66 | } |
| 67 | |
| 68 | static 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 | |
Adrian Bunk | d130d97 | 2008-09-20 10:25:20 +0200 | [diff] [blame] | 73 | static struct ad7414_data *ad7414_update_device(struct device *dev) |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 74 | { |
Axel Lin | 045c139 | 2014-07-01 22:31:44 +0800 | [diff] [blame] | 75 | struct ad7414_data *data = dev_get_drvdata(dev); |
| 76 | struct i2c_client *client = data->client; |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 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 | |
| 110 | static 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 | } |
| 116 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL, 0); |
| 117 | |
| 118 | static 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 | |
| 126 | static ssize_t set_max_min(struct device *dev, |
| 127 | struct device_attribute *attr, |
| 128 | const char *buf, size_t count) |
| 129 | { |
Axel Lin | 045c139 | 2014-07-01 22:31:44 +0800 | [diff] [blame] | 130 | struct ad7414_data *data = dev_get_drvdata(dev); |
| 131 | struct i2c_client *client = data->client; |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 132 | int index = to_sensor_dev_attr(attr)->index; |
| 133 | u8 reg = AD7414_REG_LIMIT[index]; |
Frans Meulenbroeks | dcb7cb9 | 2012-01-08 19:34:04 +0100 | [diff] [blame] | 134 | long temp; |
| 135 | int ret = kstrtol(buf, 10, &temp); |
| 136 | |
| 137 | if (ret < 0) |
| 138 | return ret; |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 139 | |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 140 | temp = clamp_val(temp, -40000, 85000); |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 141 | temp = (temp + (temp < 0 ? -500 : 500)) / 1000; |
| 142 | |
| 143 | mutex_lock(&data->lock); |
| 144 | data->temps[index] = temp; |
| 145 | ad7414_write(client, reg, temp); |
| 146 | mutex_unlock(&data->lock); |
| 147 | return count; |
| 148 | } |
| 149 | |
| 150 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, |
| 151 | show_max_min, set_max_min, 0); |
| 152 | static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, |
| 153 | show_max_min, set_max_min, 1); |
| 154 | |
| 155 | static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, |
| 156 | char *buf) |
| 157 | { |
| 158 | int bitnr = to_sensor_dev_attr(attr)->index; |
| 159 | struct ad7414_data *data = ad7414_update_device(dev); |
| 160 | int value = (data->temp_input >> bitnr) & 1; |
| 161 | return sprintf(buf, "%d\n", value); |
| 162 | } |
| 163 | |
| 164 | static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 3); |
| 165 | static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 4); |
| 166 | |
Axel Lin | 045c139 | 2014-07-01 22:31:44 +0800 | [diff] [blame] | 167 | static struct attribute *ad7414_attrs[] = { |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 168 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 169 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 170 | &sensor_dev_attr_temp1_min.dev_attr.attr, |
| 171 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, |
| 172 | &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, |
| 173 | NULL |
| 174 | }; |
| 175 | |
Axel Lin | 045c139 | 2014-07-01 22:31:44 +0800 | [diff] [blame] | 176 | ATTRIBUTE_GROUPS(ad7414); |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 177 | |
| 178 | static int ad7414_probe(struct i2c_client *client, |
| 179 | const struct i2c_device_id *dev_id) |
| 180 | { |
Axel Lin | 045c139 | 2014-07-01 22:31:44 +0800 | [diff] [blame] | 181 | struct device *dev = &client->dev; |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 182 | struct ad7414_data *data; |
Axel Lin | 045c139 | 2014-07-01 22:31:44 +0800 | [diff] [blame] | 183 | struct device *hwmon_dev; |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 184 | int conf; |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 185 | |
| 186 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA | |
Guenter Roeck | 0fee659 | 2012-06-02 09:57:57 -0700 | [diff] [blame] | 187 | I2C_FUNC_SMBUS_READ_WORD_DATA)) |
| 188 | return -EOPNOTSUPP; |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 189 | |
Axel Lin | 045c139 | 2014-07-01 22:31:44 +0800 | [diff] [blame] | 190 | data = devm_kzalloc(dev, sizeof(struct ad7414_data), GFP_KERNEL); |
Guenter Roeck | 0fee659 | 2012-06-02 09:57:57 -0700 | [diff] [blame] | 191 | if (!data) |
| 192 | return -ENOMEM; |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 193 | |
Axel Lin | 045c139 | 2014-07-01 22:31:44 +0800 | [diff] [blame] | 194 | data->client = client; |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 195 | mutex_init(&data->lock); |
| 196 | |
| 197 | dev_info(&client->dev, "chip found\n"); |
| 198 | |
| 199 | /* Make sure the chip is powered up. */ |
| 200 | conf = i2c_smbus_read_byte_data(client, AD7414_REG_CONF); |
| 201 | if (conf < 0) |
Axel Lin | 045c139 | 2014-07-01 22:31:44 +0800 | [diff] [blame] | 202 | dev_warn(dev, "ad7414_probe unable to read config register.\n"); |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 203 | else { |
| 204 | conf &= ~(1 << 7); |
| 205 | i2c_smbus_write_byte_data(client, AD7414_REG_CONF, conf); |
| 206 | } |
| 207 | |
Axel Lin | 045c139 | 2014-07-01 22:31:44 +0800 | [diff] [blame] | 208 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, |
| 209 | client->name, |
| 210 | data, ad7414_groups); |
| 211 | return PTR_ERR_OR_ZERO(hwmon_dev); |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | static const struct i2c_device_id ad7414_id[] = { |
| 215 | { "ad7414", 0 }, |
| 216 | {} |
| 217 | }; |
axel lin | 6407deb | 2011-02-24 02:20:37 +0000 | [diff] [blame] | 218 | MODULE_DEVICE_TABLE(i2c, ad7414_id); |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 219 | |
| 220 | static struct i2c_driver ad7414_driver = { |
| 221 | .driver = { |
| 222 | .name = "ad7414", |
| 223 | }, |
| 224 | .probe = ad7414_probe, |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 225 | .id_table = ad7414_id, |
| 226 | }; |
| 227 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 228 | module_i2c_driver(ad7414_driver); |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 229 | |
| 230 | MODULE_AUTHOR("Stefan Roese <sr at denx.de>, " |
| 231 | "Frank Edelhaeuser <frank.edelhaeuser at spansion.com>"); |
| 232 | |
| 233 | MODULE_DESCRIPTION("AD7414 driver"); |
| 234 | MODULE_LICENSE("GPL"); |