blob: d78bdcf467f3e66fb316d95ec66fbfe8a0128a58 [file] [log] [blame]
Guenter Roeck200855e2011-07-29 22:21:53 -07001/*
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
Guenter Roeckbc581e62011-10-01 16:50:36 -070031enum chips { zl2004, zl2005, zl2006, zl2008, zl2105, zl2106, zl6100, zl6105 };
Guenter Roeck200855e2011-07-29 22:21:53 -070032
33struct 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_DEVICE_ID 0xe4
42
43#define ZL6100_WAIT_TIME 1000 /* uS */
44
45static ushort delay = ZL6100_WAIT_TIME;
46module_param(delay, ushort, 0644);
47MODULE_PARM_DESC(delay, "Delay between chip accesses in uS");
48
49/* Some chips need a delay between accesses */
50static inline void zl6100_wait(const struct zl6100_data *data)
51{
52 if (delay) {
53 s64 delta = ktime_us_delta(ktime_get(), data->access);
54 if (delta < delay)
55 udelay(delay - delta);
56 }
57}
58
59static int zl6100_read_word_data(struct i2c_client *client, int page, int reg)
60{
61 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
62 struct zl6100_data *data = to_zl6100_data(info);
63 int ret;
64
65 if (page || reg >= PMBUS_VIRT_BASE)
66 return -ENXIO;
67
Guenter Roeckbc581e62011-10-01 16:50:36 -070068 if (data->id == zl2005) {
69 /*
70 * Limit register detection is not reliable on ZL2005.
71 * Make sure registers are not erroneously detected.
72 */
73 switch (reg) {
74 case PMBUS_VOUT_OV_WARN_LIMIT:
75 case PMBUS_VOUT_UV_WARN_LIMIT:
76 case PMBUS_IOUT_OC_WARN_LIMIT:
77 return -ENXIO;
78 }
79 }
80
Guenter Roeck200855e2011-07-29 22:21:53 -070081 zl6100_wait(data);
82 ret = pmbus_read_word_data(client, page, reg);
83 data->access = ktime_get();
84
85 return ret;
86}
87
88static int zl6100_read_byte_data(struct i2c_client *client, int page, int reg)
89{
90 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
91 struct zl6100_data *data = to_zl6100_data(info);
92 int ret;
93
94 if (page > 0)
95 return -ENXIO;
96
97 zl6100_wait(data);
98 ret = pmbus_read_byte_data(client, page, reg);
99 data->access = ktime_get();
100
101 return ret;
102}
103
104static int zl6100_write_word_data(struct i2c_client *client, int page, int reg,
105 u16 word)
106{
107 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
108 struct zl6100_data *data = to_zl6100_data(info);
109 int ret;
110
111 if (page || reg >= PMBUS_VIRT_BASE)
112 return -ENXIO;
113
114 zl6100_wait(data);
115 ret = pmbus_write_word_data(client, page, reg, word);
116 data->access = ktime_get();
117
118 return ret;
119}
120
121static int zl6100_write_byte(struct i2c_client *client, int page, u8 value)
122{
123 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
124 struct zl6100_data *data = to_zl6100_data(info);
125 int ret;
126
127 if (page > 0)
128 return -ENXIO;
129
130 zl6100_wait(data);
131 ret = pmbus_write_byte(client, page, value);
132 data->access = ktime_get();
133
134 return ret;
135}
136
137static const struct i2c_device_id zl6100_id[] = {
138 {"zl2004", zl2004},
Guenter Roeckbc581e62011-10-01 16:50:36 -0700139 {"zl2005", zl2005},
Guenter Roeck200855e2011-07-29 22:21:53 -0700140 {"zl2006", zl2006},
141 {"zl2008", zl2008},
142 {"zl2105", zl2105},
143 {"zl2106", zl2106},
144 {"zl6100", zl6100},
145 {"zl6105", zl6105},
146 { }
147};
148MODULE_DEVICE_TABLE(i2c, zl6100_id);
149
150static int zl6100_probe(struct i2c_client *client,
151 const struct i2c_device_id *id)
152{
153 int ret;
154 struct zl6100_data *data;
155 struct pmbus_driver_info *info;
156 u8 device_id[I2C_SMBUS_BLOCK_MAX + 1];
157 const struct i2c_device_id *mid;
158
159 if (!i2c_check_functionality(client->adapter,
160 I2C_FUNC_SMBUS_READ_BYTE_DATA
161 | I2C_FUNC_SMBUS_READ_BLOCK_DATA))
162 return -ENODEV;
163
164 ret = i2c_smbus_read_block_data(client, ZL6100_DEVICE_ID,
165 device_id);
166 if (ret < 0) {
167 dev_err(&client->dev, "Failed to read device ID\n");
168 return ret;
169 }
170 device_id[ret] = '\0';
171 dev_info(&client->dev, "Device ID %s\n", device_id);
172
173 mid = NULL;
174 for (mid = zl6100_id; mid->name[0]; mid++) {
175 if (!strncasecmp(mid->name, device_id, strlen(mid->name)))
176 break;
177 }
178 if (!mid->name[0]) {
179 dev_err(&client->dev, "Unsupported device\n");
180 return -ENODEV;
181 }
182 if (id->driver_data != mid->driver_data)
183 dev_notice(&client->dev,
184 "Device mismatch: Configured %s, detected %s\n",
185 id->name, mid->name);
186
187 data = kzalloc(sizeof(struct zl6100_data), GFP_KERNEL);
188 if (!data)
189 return -ENOMEM;
190
191 data->id = mid->driver_data;
192
193 /*
Guenter Roeckbc581e62011-10-01 16:50:36 -0700194 * ZL2005, ZL2008, ZL2105, and ZL6100 are known to require a wait time
Guenter Roeck200855e2011-07-29 22:21:53 -0700195 * between I2C accesses. ZL2004 and ZL6105 are known to be safe.
Guenter Roeckbc581e62011-10-01 16:50:36 -0700196 * Other chips have not yet been tested.
Guenter Roeck200855e2011-07-29 22:21:53 -0700197 *
198 * Only clear the wait time for chips known to be safe. The wait time
199 * can be cleared later for additional chips if tests show that it
200 * is not needed (in other words, better be safe than sorry).
201 */
202 if (data->id == zl2004 || data->id == zl6105)
203 delay = 0;
204
205 /*
206 * Since there was a direct I2C device access above, wait before
207 * accessing the chip again.
208 * Set the timestamp, wait, then set it again. This should provide
209 * enough buffer time to be safe.
210 */
211 data->access = ktime_get();
212 zl6100_wait(data);
213 data->access = ktime_get();
214
215 info = &data->info;
216
217 info->pages = 1;
218 info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT
219 | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
220 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
221 | PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP;
222
223 info->read_word_data = zl6100_read_word_data;
224 info->read_byte_data = zl6100_read_byte_data;
225 info->write_word_data = zl6100_write_word_data;
226 info->write_byte = zl6100_write_byte;
227
228 ret = pmbus_do_probe(client, mid, info);
229 if (ret)
230 goto err_mem;
231 return 0;
232
233err_mem:
234 kfree(data);
235 return ret;
236}
237
238static int zl6100_remove(struct i2c_client *client)
239{
240 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
241 const struct zl6100_data *data = to_zl6100_data(info);
242
243 pmbus_do_remove(client);
244 kfree(data);
245 return 0;
246}
247
248static struct i2c_driver zl6100_driver = {
249 .driver = {
250 .name = "zl6100",
251 },
252 .probe = zl6100_probe,
253 .remove = zl6100_remove,
254 .id_table = zl6100_id,
255};
256
257static int __init zl6100_init(void)
258{
259 return i2c_add_driver(&zl6100_driver);
260}
261
262static void __exit zl6100_exit(void)
263{
264 i2c_del_driver(&zl6100_driver);
265}
266
267MODULE_AUTHOR("Guenter Roeck");
268MODULE_DESCRIPTION("PMBus driver for ZL6100 and compatibles");
269MODULE_LICENSE("GPL");
270module_init(zl6100_init);
271module_exit(zl6100_exit);