Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 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> |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 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 | |
| 35 | static u8 AD7414_REG_LIMIT[] = { AD7414_REG_T_HIGH, AD7414_REG_T_LOW }; |
| 36 | |
| 37 | struct ad7414_data { |
Axel Lin | 045c139 | 2014-07-01 22:31:44 +0800 | [diff] [blame] | 38 | struct i2c_client *client; |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 39 | struct mutex lock; /* atomic read data updates */ |
| 40 | char valid; /* !=0 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 */ |
| 47 | static inline int ad7414_temp_from_reg(s16 reg) |
| 48 | { |
Guenter Roeck | 8deeac8 | 2012-01-19 11:02:13 -0800 | [diff] [blame] | 49 | /* |
| 50 | * use integer division instead of equivalent right shift to |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 51 | * guarantee arithmetic shift and preserve the sign |
| 52 | */ |
| 53 | return ((int)reg / 64) * 250; |
| 54 | } |
| 55 | |
| 56 | static inline int ad7414_read(struct i2c_client *client, u8 reg) |
| 57 | { |
Jean Delvare | 90f4102 | 2011-11-04 12:00:47 +0100 | [diff] [blame] | 58 | if (reg == AD7414_REG_TEMP) |
| 59 | return i2c_smbus_read_word_swapped(client, reg); |
| 60 | else |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 61 | return i2c_smbus_read_byte_data(client, reg); |
| 62 | } |
| 63 | |
| 64 | static 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 | |
Adrian Bunk | d130d97 | 2008-09-20 10:25:20 +0200 | [diff] [blame] | 69 | static struct ad7414_data *ad7414_update_device(struct device *dev) |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 70 | { |
Axel Lin | 045c139 | 2014-07-01 22:31:44 +0800 | [diff] [blame] | 71 | struct ad7414_data *data = dev_get_drvdata(dev); |
| 72 | struct i2c_client *client = data->client; |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 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 = 1; |
| 99 | } |
| 100 | |
| 101 | mutex_unlock(&data->lock); |
| 102 | |
| 103 | return data; |
| 104 | } |
| 105 | |
Guenter Roeck | cbf6cb2 | 2018-12-10 14:01:59 -0800 | [diff] [blame] | 106 | static ssize_t temp_input_show(struct device *dev, |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 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 | } |
Guenter Roeck | cbf6cb2 | 2018-12-10 14:01:59 -0800 | [diff] [blame] | 112 | static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_input, 0); |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 113 | |
Guenter Roeck | cbf6cb2 | 2018-12-10 14:01:59 -0800 | [diff] [blame] | 114 | static ssize_t max_min_show(struct device *dev, struct device_attribute *attr, |
| 115 | char *buf) |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 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 | |
Guenter Roeck | cbf6cb2 | 2018-12-10 14:01:59 -0800 | [diff] [blame] | 122 | static ssize_t max_min_store(struct device *dev, |
| 123 | struct device_attribute *attr, const char *buf, |
| 124 | size_t count) |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 125 | { |
Axel Lin | 045c139 | 2014-07-01 22:31:44 +0800 | [diff] [blame] | 126 | struct ad7414_data *data = dev_get_drvdata(dev); |
| 127 | struct i2c_client *client = data->client; |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 128 | int index = to_sensor_dev_attr(attr)->index; |
| 129 | u8 reg = AD7414_REG_LIMIT[index]; |
Frans Meulenbroeks | dcb7cb9 | 2012-01-08 19:34:04 +0100 | [diff] [blame] | 130 | long temp; |
| 131 | int ret = kstrtol(buf, 10, &temp); |
| 132 | |
| 133 | if (ret < 0) |
| 134 | return ret; |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 135 | |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 136 | temp = clamp_val(temp, -40000, 85000); |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 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 | |
Guenter Roeck | cbf6cb2 | 2018-12-10 14:01:59 -0800 | [diff] [blame] | 146 | static SENSOR_DEVICE_ATTR_RW(temp1_max, max_min, 0); |
| 147 | static SENSOR_DEVICE_ATTR_RW(temp1_min, max_min, 1); |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 148 | |
Guenter Roeck | cbf6cb2 | 2018-12-10 14:01:59 -0800 | [diff] [blame] | 149 | static ssize_t alarm_show(struct device *dev, struct device_attribute *attr, |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 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 | |
Guenter Roeck | cbf6cb2 | 2018-12-10 14:01:59 -0800 | [diff] [blame] | 158 | static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, alarm, 3); |
| 159 | static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 4); |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 160 | |
Axel Lin | 045c139 | 2014-07-01 22:31:44 +0800 | [diff] [blame] | 161 | static struct attribute *ad7414_attrs[] = { |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 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 | |
Axel Lin | 045c139 | 2014-07-01 22:31:44 +0800 | [diff] [blame] | 170 | ATTRIBUTE_GROUPS(ad7414); |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 171 | |
| 172 | static int ad7414_probe(struct i2c_client *client, |
| 173 | const struct i2c_device_id *dev_id) |
| 174 | { |
Axel Lin | 045c139 | 2014-07-01 22:31:44 +0800 | [diff] [blame] | 175 | struct device *dev = &client->dev; |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 176 | struct ad7414_data *data; |
Axel Lin | 045c139 | 2014-07-01 22:31:44 +0800 | [diff] [blame] | 177 | struct device *hwmon_dev; |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 178 | int conf; |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 179 | |
| 180 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA | |
Guenter Roeck | 0fee659 | 2012-06-02 09:57:57 -0700 | [diff] [blame] | 181 | I2C_FUNC_SMBUS_READ_WORD_DATA)) |
| 182 | return -EOPNOTSUPP; |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 183 | |
Axel Lin | 045c139 | 2014-07-01 22:31:44 +0800 | [diff] [blame] | 184 | data = devm_kzalloc(dev, sizeof(struct ad7414_data), GFP_KERNEL); |
Guenter Roeck | 0fee659 | 2012-06-02 09:57:57 -0700 | [diff] [blame] | 185 | if (!data) |
| 186 | return -ENOMEM; |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 187 | |
Axel Lin | 045c139 | 2014-07-01 22:31:44 +0800 | [diff] [blame] | 188 | data->client = client; |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 189 | mutex_init(&data->lock); |
| 190 | |
| 191 | dev_info(&client->dev, "chip found\n"); |
| 192 | |
| 193 | /* Make sure the chip is powered up. */ |
| 194 | conf = i2c_smbus_read_byte_data(client, AD7414_REG_CONF); |
| 195 | if (conf < 0) |
Axel Lin | 045c139 | 2014-07-01 22:31:44 +0800 | [diff] [blame] | 196 | dev_warn(dev, "ad7414_probe unable to read config register.\n"); |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 197 | else { |
| 198 | conf &= ~(1 << 7); |
| 199 | i2c_smbus_write_byte_data(client, AD7414_REG_CONF, conf); |
| 200 | } |
| 201 | |
Axel Lin | 045c139 | 2014-07-01 22:31:44 +0800 | [diff] [blame] | 202 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, |
| 203 | client->name, |
| 204 | data, ad7414_groups); |
| 205 | return PTR_ERR_OR_ZERO(hwmon_dev); |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | static const struct i2c_device_id ad7414_id[] = { |
| 209 | { "ad7414", 0 }, |
| 210 | {} |
| 211 | }; |
axel lin | 6407deb | 2011-02-24 02:20:37 +0000 | [diff] [blame] | 212 | MODULE_DEVICE_TABLE(i2c, ad7414_id); |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 213 | |
Guenter Roeck | 0718298 | 2019-04-04 06:26:08 -0700 | [diff] [blame] | 214 | static const struct of_device_id __maybe_unused ad7414_of_match[] = { |
Javier Martinez Canillas | 72edf31 | 2017-02-24 10:12:54 -0300 | [diff] [blame] | 215 | { .compatible = "ad,ad7414" }, |
| 216 | { }, |
| 217 | }; |
| 218 | MODULE_DEVICE_TABLE(of, ad7414_of_match); |
| 219 | |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 220 | static struct i2c_driver ad7414_driver = { |
| 221 | .driver = { |
| 222 | .name = "ad7414", |
Javier Martinez Canillas | 72edf31 | 2017-02-24 10:12:54 -0300 | [diff] [blame] | 223 | .of_match_table = of_match_ptr(ad7414_of_match), |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 224 | }, |
| 225 | .probe = ad7414_probe, |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 226 | .id_table = ad7414_id, |
| 227 | }; |
| 228 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 229 | module_i2c_driver(ad7414_driver); |
Sean MacLennan | 6c633c3 | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 230 | |
| 231 | MODULE_AUTHOR("Stefan Roese <sr at denx.de>, " |
| 232 | "Frank Edelhaeuser <frank.edelhaeuser at spansion.com>"); |
| 233 | |
| 234 | MODULE_DESCRIPTION("AD7414 driver"); |
| 235 | MODULE_LICENSE("GPL"); |