Blame view

drivers/hwmon/ad7414.c 6.14 KB
2874c5fd2   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
6c633c302   Sean MacLennan   hwmon: ad7414 driver
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  /*
   * An hwmon driver for the Analog Devices AD7414
   *
   * Copyright 2006 Stefan Roese <sr at denx.de>, DENX Software Engineering
   *
   * Copyright (c) 2008 PIKA Technologies
   *   Sean MacLennan <smaclennan@pikatech.com>
   *
   * Copyright (c) 2008 Spansion Inc.
   *   Frank Edelhaeuser <frank.edelhaeuser at spansion.com>
   *   (converted to "new style" I2C driver model, removed checkpatch.pl warnings)
   *
   * Based on ad7418.c
   * Copyright 2006 Tower Technologies, Alessandro Zummo <a.zummo at towertech.it>
6c633c302   Sean MacLennan   hwmon: ad7414 driver
16
17
18
19
20
21
22
23
24
25
   */
  
  #include <linux/module.h>
  #include <linux/jiffies.h>
  #include <linux/i2c.h>
  #include <linux/hwmon.h>
  #include <linux/hwmon-sysfs.h>
  #include <linux/err.h>
  #include <linux/mutex.h>
  #include <linux/sysfs.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
26
  #include <linux/slab.h>
6c633c302   Sean MacLennan   hwmon: ad7414 driver
27
28
29
30
31
32
33
34
35
36
37
  
  
  /* AD7414 registers */
  #define AD7414_REG_TEMP		0x00
  #define AD7414_REG_CONF		0x01
  #define AD7414_REG_T_HIGH	0x02
  #define AD7414_REG_T_LOW	0x03
  
  static u8 AD7414_REG_LIMIT[] = { AD7414_REG_T_HIGH, AD7414_REG_T_LOW };
  
  struct ad7414_data {
045c1391e   Axel Lin   hwmon: (ad7414) C...
38
  	struct i2c_client	*client;
6c633c302   Sean MacLennan   hwmon: ad7414 driver
39
40
41
42
43
44
45
46
47
48
  	struct mutex		lock;	/* atomic read data updates */
  	char			valid;	/* !=0 if following fields are valid */
  	unsigned long		next_update;	/* In jiffies */
  	s16			temp_input;	/* Register values */
  	s8			temps[ARRAY_SIZE(AD7414_REG_LIMIT)];
  };
  
  /* REG: (0.25C/bit, two's complement) << 6 */
  static inline int ad7414_temp_from_reg(s16 reg)
  {
8deeac82b   Guenter Roeck   hwmon: (ad7414) F...
49
50
  	/*
  	 * use integer division instead of equivalent right shift to
6c633c302   Sean MacLennan   hwmon: ad7414 driver
51
52
53
54
55
56
57
  	 * guarantee arithmetic shift and preserve the sign
  	 */
  	return ((int)reg / 64) * 250;
  }
  
  static inline int ad7414_read(struct i2c_client *client, u8 reg)
  {
90f4102ce   Jean Delvare   hwmon: Use i2c_sm...
58
59
60
  	if (reg == AD7414_REG_TEMP)
  		return i2c_smbus_read_word_swapped(client, reg);
  	else
6c633c302   Sean MacLennan   hwmon: ad7414 driver
61
62
63
64
65
66
67
  		return i2c_smbus_read_byte_data(client, reg);
  }
  
  static inline int ad7414_write(struct i2c_client *client, u8 reg, u8 value)
  {
  	return i2c_smbus_write_byte_data(client, reg, value);
  }
d130d9715   Adrian Bunk   hwmon: (ad7414) M...
68
  static struct ad7414_data *ad7414_update_device(struct device *dev)
6c633c302   Sean MacLennan   hwmon: ad7414 driver
69
  {
045c1391e   Axel Lin   hwmon: (ad7414) C...
70
71
  	struct ad7414_data *data = dev_get_drvdata(dev);
  	struct i2c_client *client = data->client;
6c633c302   Sean MacLennan   hwmon: ad7414 driver
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
  
  	mutex_lock(&data->lock);
  
  	if (time_after(jiffies, data->next_update) || !data->valid) {
  		int value, i;
  
  		dev_dbg(&client->dev, "starting ad7414 update
  ");
  
  		value = ad7414_read(client, AD7414_REG_TEMP);
  		if (value < 0)
  			dev_dbg(&client->dev, "AD7414_REG_TEMP err %d
  ",
  				value);
  		else
  			data->temp_input = value;
  
  		for (i = 0; i < ARRAY_SIZE(AD7414_REG_LIMIT); ++i) {
  			value = ad7414_read(client, AD7414_REG_LIMIT[i]);
  			if (value < 0)
  				dev_dbg(&client->dev, "AD7414 reg %d err %d
  ",
  					AD7414_REG_LIMIT[i], value);
  			else
  				data->temps[i] = value;
  		}
  
  		data->next_update = jiffies + HZ + HZ / 2;
  		data->valid = 1;
  	}
  
  	mutex_unlock(&data->lock);
  
  	return data;
  }
cbf6cb2b7   Guenter Roeck   hwmon: (ad7414) U...
107
  static ssize_t temp_input_show(struct device *dev,
6c633c302   Sean MacLennan   hwmon: ad7414 driver
108
109
110
111
112
113
  			       struct device_attribute *attr, char *buf)
  {
  	struct ad7414_data *data = ad7414_update_device(dev);
  	return sprintf(buf, "%d
  ", ad7414_temp_from_reg(data->temp_input));
  }
cbf6cb2b7   Guenter Roeck   hwmon: (ad7414) U...
114
  static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_input, 0);
6c633c302   Sean MacLennan   hwmon: ad7414 driver
115

cbf6cb2b7   Guenter Roeck   hwmon: (ad7414) U...
116
117
  static ssize_t max_min_show(struct device *dev, struct device_attribute *attr,
  			    char *buf)
6c633c302   Sean MacLennan   hwmon: ad7414 driver
118
119
120
121
122
123
  {
  	int index = to_sensor_dev_attr(attr)->index;
  	struct ad7414_data *data = ad7414_update_device(dev);
  	return sprintf(buf, "%d
  ", data->temps[index] * 1000);
  }
cbf6cb2b7   Guenter Roeck   hwmon: (ad7414) U...
124
125
126
  static ssize_t max_min_store(struct device *dev,
  			     struct device_attribute *attr, const char *buf,
  			     size_t count)
6c633c302   Sean MacLennan   hwmon: ad7414 driver
127
  {
045c1391e   Axel Lin   hwmon: (ad7414) C...
128
129
  	struct ad7414_data *data = dev_get_drvdata(dev);
  	struct i2c_client *client = data->client;
6c633c302   Sean MacLennan   hwmon: ad7414 driver
130
131
  	int index = to_sensor_dev_attr(attr)->index;
  	u8 reg = AD7414_REG_LIMIT[index];
dcb7cb97b   Frans Meulenbroeks   hwmon: (ad7414) f...
132
133
134
135
136
  	long temp;
  	int ret = kstrtol(buf, 10, &temp);
  
  	if (ret < 0)
  		return ret;
6c633c302   Sean MacLennan   hwmon: ad7414 driver
137

2a844c148   Guenter Roeck   hwmon: Replace SE...
138
  	temp = clamp_val(temp, -40000, 85000);
6c633c302   Sean MacLennan   hwmon: ad7414 driver
139
140
141
142
143
144
145
146
  	temp = (temp + (temp < 0 ? -500 : 500)) / 1000;
  
  	mutex_lock(&data->lock);
  	data->temps[index] = temp;
  	ad7414_write(client, reg, temp);
  	mutex_unlock(&data->lock);
  	return count;
  }
cbf6cb2b7   Guenter Roeck   hwmon: (ad7414) U...
147
148
  static SENSOR_DEVICE_ATTR_RW(temp1_max, max_min, 0);
  static SENSOR_DEVICE_ATTR_RW(temp1_min, max_min, 1);
6c633c302   Sean MacLennan   hwmon: ad7414 driver
149

cbf6cb2b7   Guenter Roeck   hwmon: (ad7414) U...
150
  static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
6c633c302   Sean MacLennan   hwmon: ad7414 driver
151
152
153
154
155
156
157
158
  			  char *buf)
  {
  	int bitnr = to_sensor_dev_attr(attr)->index;
  	struct ad7414_data *data = ad7414_update_device(dev);
  	int value = (data->temp_input >> bitnr) & 1;
  	return sprintf(buf, "%d
  ", value);
  }
cbf6cb2b7   Guenter Roeck   hwmon: (ad7414) U...
159
160
  static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, alarm, 3);
  static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 4);
6c633c302   Sean MacLennan   hwmon: ad7414 driver
161

045c1391e   Axel Lin   hwmon: (ad7414) C...
162
  static struct attribute *ad7414_attrs[] = {
6c633c302   Sean MacLennan   hwmon: ad7414 driver
163
164
165
166
167
168
169
  	&sensor_dev_attr_temp1_input.dev_attr.attr,
  	&sensor_dev_attr_temp1_max.dev_attr.attr,
  	&sensor_dev_attr_temp1_min.dev_attr.attr,
  	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  	NULL
  };
045c1391e   Axel Lin   hwmon: (ad7414) C...
170
  ATTRIBUTE_GROUPS(ad7414);
6c633c302   Sean MacLennan   hwmon: ad7414 driver
171

674870385   Stephen Kitt   hwmon: use simple...
172
  static int ad7414_probe(struct i2c_client *client)
6c633c302   Sean MacLennan   hwmon: ad7414 driver
173
  {
045c1391e   Axel Lin   hwmon: (ad7414) C...
174
  	struct device *dev = &client->dev;
6c633c302   Sean MacLennan   hwmon: ad7414 driver
175
  	struct ad7414_data *data;
045c1391e   Axel Lin   hwmon: (ad7414) C...
176
  	struct device *hwmon_dev;
6c633c302   Sean MacLennan   hwmon: ad7414 driver
177
  	int conf;
6c633c302   Sean MacLennan   hwmon: ad7414 driver
178
179
  
  	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA |
0fee6591b   Guenter Roeck   hwmon: (ad7414) C...
180
181
  				     I2C_FUNC_SMBUS_READ_WORD_DATA))
  		return -EOPNOTSUPP;
6c633c302   Sean MacLennan   hwmon: ad7414 driver
182

045c1391e   Axel Lin   hwmon: (ad7414) C...
183
  	data = devm_kzalloc(dev, sizeof(struct ad7414_data), GFP_KERNEL);
0fee6591b   Guenter Roeck   hwmon: (ad7414) C...
184
185
  	if (!data)
  		return -ENOMEM;
6c633c302   Sean MacLennan   hwmon: ad7414 driver
186

045c1391e   Axel Lin   hwmon: (ad7414) C...
187
  	data->client = client;
6c633c302   Sean MacLennan   hwmon: ad7414 driver
188
189
190
191
192
193
194
195
  	mutex_init(&data->lock);
  
  	dev_info(&client->dev, "chip found
  ");
  
  	/* Make sure the chip is powered up. */
  	conf = i2c_smbus_read_byte_data(client, AD7414_REG_CONF);
  	if (conf < 0)
045c1391e   Axel Lin   hwmon: (ad7414) C...
196
197
  		dev_warn(dev, "ad7414_probe unable to read config register.
  ");
6c633c302   Sean MacLennan   hwmon: ad7414 driver
198
199
200
201
  	else {
  		conf &= ~(1 << 7);
  		i2c_smbus_write_byte_data(client, AD7414_REG_CONF, conf);
  	}
045c1391e   Axel Lin   hwmon: (ad7414) C...
202
203
204
205
  	hwmon_dev = devm_hwmon_device_register_with_groups(dev,
  							   client->name,
  							   data, ad7414_groups);
  	return PTR_ERR_OR_ZERO(hwmon_dev);
6c633c302   Sean MacLennan   hwmon: ad7414 driver
206
207
208
209
210
211
  }
  
  static const struct i2c_device_id ad7414_id[] = {
  	{ "ad7414", 0 },
  	{}
  };
6407deb59   Axel Lin   hwmon: (ad7414) a...
212
  MODULE_DEVICE_TABLE(i2c, ad7414_id);
6c633c302   Sean MacLennan   hwmon: ad7414 driver
213

071829868   Guenter Roeck   hwmon: (ad7414) F...
214
  static const struct of_device_id __maybe_unused ad7414_of_match[] = {
72edf311e   Javier Martinez Canillas   hwmon: (ad7414) A...
215
216
217
218
  	{ .compatible = "ad,ad7414" },
  	{ },
  };
  MODULE_DEVICE_TABLE(of, ad7414_of_match);
6c633c302   Sean MacLennan   hwmon: ad7414 driver
219
220
221
  static struct i2c_driver ad7414_driver = {
  	.driver = {
  		.name	= "ad7414",
72edf311e   Javier Martinez Canillas   hwmon: (ad7414) A...
222
  		.of_match_table = of_match_ptr(ad7414_of_match),
6c633c302   Sean MacLennan   hwmon: ad7414 driver
223
  	},
674870385   Stephen Kitt   hwmon: use simple...
224
  	.probe_new = ad7414_probe,
6c633c302   Sean MacLennan   hwmon: ad7414 driver
225
226
  	.id_table = ad7414_id,
  };
f0967eea8   Axel Lin   hwmon: convert dr...
227
  module_i2c_driver(ad7414_driver);
6c633c302   Sean MacLennan   hwmon: ad7414 driver
228
229
230
231
232
233
  
  MODULE_AUTHOR("Stefan Roese <sr at denx.de>, "
  	      "Frank Edelhaeuser <frank.edelhaeuser at spansion.com>");
  
  MODULE_DESCRIPTION("AD7414 driver");
  MODULE_LICENSE("GPL");