]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/hwmon/pmbus/zl6100.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/linux...
[mirror_ubuntu-artful-kernel.git] / drivers / hwmon / pmbus / zl6100.c
1 /*
2 * Hardware monitoring driver for ZL6100 and compatibles
3 *
4 * Copyright (c) 2011 Ericsson AB.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/err.h>
25 #include <linux/slab.h>
26 #include <linux/i2c.h>
27 #include <linux/ktime.h>
28 #include <linux/delay.h>
29 #include "pmbus.h"
30
31 enum chips { zl2004, zl2005, zl2006, zl2008, zl2105, zl2106, zl6100, zl6105 };
32
33 struct zl6100_data {
34 int id;
35 ktime_t access; /* chip access time */
36 struct pmbus_driver_info info;
37 };
38
39 #define to_zl6100_data(x) container_of(x, struct zl6100_data, info)
40
41 #define ZL6100_MFR_CONFIG 0xd0
42 #define ZL6100_DEVICE_ID 0xe4
43
44 #define ZL6100_MFR_XTEMP_ENABLE (1 << 7)
45
46 #define ZL6100_WAIT_TIME 1000 /* uS */
47
48 static ushort delay = ZL6100_WAIT_TIME;
49 module_param(delay, ushort, 0644);
50 MODULE_PARM_DESC(delay, "Delay between chip accesses in uS");
51
52 /* Some chips need a delay between accesses */
53 static inline void zl6100_wait(const struct zl6100_data *data)
54 {
55 if (delay) {
56 s64 delta = ktime_us_delta(ktime_get(), data->access);
57 if (delta < delay)
58 udelay(delay - delta);
59 }
60 }
61
62 static int zl6100_read_word_data(struct i2c_client *client, int page, int reg)
63 {
64 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
65 struct zl6100_data *data = to_zl6100_data(info);
66 int ret;
67
68 if (page || reg >= PMBUS_VIRT_BASE)
69 return -ENXIO;
70
71 if (data->id == zl2005) {
72 /*
73 * Limit register detection is not reliable on ZL2005.
74 * Make sure registers are not erroneously detected.
75 */
76 switch (reg) {
77 case PMBUS_VOUT_OV_WARN_LIMIT:
78 case PMBUS_VOUT_UV_WARN_LIMIT:
79 case PMBUS_IOUT_OC_WARN_LIMIT:
80 return -ENXIO;
81 }
82 }
83
84 zl6100_wait(data);
85 ret = pmbus_read_word_data(client, page, reg);
86 data->access = ktime_get();
87
88 return ret;
89 }
90
91 static int zl6100_read_byte_data(struct i2c_client *client, int page, int reg)
92 {
93 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
94 struct zl6100_data *data = to_zl6100_data(info);
95 int ret;
96
97 if (page > 0)
98 return -ENXIO;
99
100 zl6100_wait(data);
101 ret = pmbus_read_byte_data(client, page, reg);
102 data->access = ktime_get();
103
104 return ret;
105 }
106
107 static int zl6100_write_word_data(struct i2c_client *client, int page, int reg,
108 u16 word)
109 {
110 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
111 struct zl6100_data *data = to_zl6100_data(info);
112 int ret;
113
114 if (page || reg >= PMBUS_VIRT_BASE)
115 return -ENXIO;
116
117 zl6100_wait(data);
118 ret = pmbus_write_word_data(client, page, reg, word);
119 data->access = ktime_get();
120
121 return ret;
122 }
123
124 static int zl6100_write_byte(struct i2c_client *client, int page, u8 value)
125 {
126 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
127 struct zl6100_data *data = to_zl6100_data(info);
128 int ret;
129
130 if (page > 0)
131 return -ENXIO;
132
133 zl6100_wait(data);
134 ret = pmbus_write_byte(client, page, value);
135 data->access = ktime_get();
136
137 return ret;
138 }
139
140 static const struct i2c_device_id zl6100_id[] = {
141 {"bmr450", zl2005},
142 {"bmr451", zl2005},
143 {"bmr462", zl2008},
144 {"bmr463", zl2008},
145 {"bmr464", zl2008},
146 {"zl2004", zl2004},
147 {"zl2005", zl2005},
148 {"zl2006", zl2006},
149 {"zl2008", zl2008},
150 {"zl2105", zl2105},
151 {"zl2106", zl2106},
152 {"zl6100", zl6100},
153 {"zl6105", zl6105},
154 { }
155 };
156 MODULE_DEVICE_TABLE(i2c, zl6100_id);
157
158 static int zl6100_probe(struct i2c_client *client,
159 const struct i2c_device_id *id)
160 {
161 int ret;
162 struct zl6100_data *data;
163 struct pmbus_driver_info *info;
164 u8 device_id[I2C_SMBUS_BLOCK_MAX + 1];
165 const struct i2c_device_id *mid;
166
167 if (!i2c_check_functionality(client->adapter,
168 I2C_FUNC_SMBUS_READ_WORD_DATA
169 | I2C_FUNC_SMBUS_READ_BLOCK_DATA))
170 return -ENODEV;
171
172 ret = i2c_smbus_read_block_data(client, ZL6100_DEVICE_ID,
173 device_id);
174 if (ret < 0) {
175 dev_err(&client->dev, "Failed to read device ID\n");
176 return ret;
177 }
178 device_id[ret] = '\0';
179 dev_info(&client->dev, "Device ID %s\n", device_id);
180
181 mid = NULL;
182 for (mid = zl6100_id; mid->name[0]; mid++) {
183 if (!strncasecmp(mid->name, device_id, strlen(mid->name)))
184 break;
185 }
186 if (!mid->name[0]) {
187 dev_err(&client->dev, "Unsupported device\n");
188 return -ENODEV;
189 }
190 if (id->driver_data != mid->driver_data)
191 dev_notice(&client->dev,
192 "Device mismatch: Configured %s, detected %s\n",
193 id->name, mid->name);
194
195 data = kzalloc(sizeof(struct zl6100_data), GFP_KERNEL);
196 if (!data)
197 return -ENOMEM;
198
199 data->id = mid->driver_data;
200
201 /*
202 * ZL2005, ZL2008, ZL2105, and ZL6100 are known to require a wait time
203 * between I2C accesses. ZL2004 and ZL6105 are known to be safe.
204 * Other chips have not yet been tested.
205 *
206 * Only clear the wait time for chips known to be safe. The wait time
207 * can be cleared later for additional chips if tests show that it
208 * is not needed (in other words, better be safe than sorry).
209 */
210 if (data->id == zl2004 || data->id == zl6105)
211 delay = 0;
212
213 /*
214 * Since there was a direct I2C device access above, wait before
215 * accessing the chip again.
216 */
217 data->access = ktime_get();
218 zl6100_wait(data);
219
220 info = &data->info;
221
222 info->pages = 1;
223 info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT
224 | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
225 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
226 | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
227
228 ret = i2c_smbus_read_word_data(client, ZL6100_MFR_CONFIG);
229 if (ret < 0)
230 goto err_mem;
231 if (ret & ZL6100_MFR_XTEMP_ENABLE)
232 info->func[0] |= PMBUS_HAVE_TEMP2;
233
234 data->access = ktime_get();
235 zl6100_wait(data);
236
237 info->read_word_data = zl6100_read_word_data;
238 info->read_byte_data = zl6100_read_byte_data;
239 info->write_word_data = zl6100_write_word_data;
240 info->write_byte = zl6100_write_byte;
241
242 ret = pmbus_do_probe(client, mid, info);
243 if (ret)
244 goto err_mem;
245 return 0;
246
247 err_mem:
248 kfree(data);
249 return ret;
250 }
251
252 static int zl6100_remove(struct i2c_client *client)
253 {
254 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
255 const struct zl6100_data *data = to_zl6100_data(info);
256
257 pmbus_do_remove(client);
258 kfree(data);
259 return 0;
260 }
261
262 static struct i2c_driver zl6100_driver = {
263 .driver = {
264 .name = "zl6100",
265 },
266 .probe = zl6100_probe,
267 .remove = zl6100_remove,
268 .id_table = zl6100_id,
269 };
270
271 static int __init zl6100_init(void)
272 {
273 return i2c_add_driver(&zl6100_driver);
274 }
275
276 static void __exit zl6100_exit(void)
277 {
278 i2c_del_driver(&zl6100_driver);
279 }
280
281 MODULE_AUTHOR("Guenter Roeck");
282 MODULE_DESCRIPTION("PMBus driver for ZL6100 and compatibles");
283 MODULE_LICENSE("GPL");
284 module_init(zl6100_init);
285 module_exit(zl6100_exit);