Commit | Line | Data |
---|---|---|
200855e5 GR |
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 | ||
bc581e6f | 31 | enum chips { zl2004, zl2005, zl2006, zl2008, zl2105, zl2106, zl6100, zl6105 }; |
200855e5 GR |
32 | |
33 | struct zl6100_data { | |
34 | int id; | |
35 | ktime_t access; /* chip access time */ | |
7ad6307a | 36 | int delay; /* Delay between chip accesses in uS */ |
200855e5 GR |
37 | struct pmbus_driver_info info; |
38 | }; | |
39 | ||
40 | #define to_zl6100_data(x) container_of(x, struct zl6100_data, info) | |
41 | ||
56badacb | 42 | #define ZL6100_MFR_CONFIG 0xd0 |
200855e5 GR |
43 | #define ZL6100_DEVICE_ID 0xe4 |
44 | ||
56badacb GR |
45 | #define ZL6100_MFR_XTEMP_ENABLE (1 << 7) |
46 | ||
200855e5 GR |
47 | #define ZL6100_WAIT_TIME 1000 /* uS */ |
48 | ||
49 | static ushort delay = ZL6100_WAIT_TIME; | |
50 | module_param(delay, ushort, 0644); | |
51 | MODULE_PARM_DESC(delay, "Delay between chip accesses in uS"); | |
52 | ||
53 | /* Some chips need a delay between accesses */ | |
54 | static inline void zl6100_wait(const struct zl6100_data *data) | |
55 | { | |
7ad6307a | 56 | if (data->delay) { |
200855e5 | 57 | s64 delta = ktime_us_delta(ktime_get(), data->access); |
7ad6307a GR |
58 | if (delta < data->delay) |
59 | udelay(data->delay - delta); | |
200855e5 GR |
60 | } |
61 | } | |
62 | ||
63 | static int zl6100_read_word_data(struct i2c_client *client, int page, int reg) | |
64 | { | |
65 | const struct pmbus_driver_info *info = pmbus_get_driver_info(client); | |
66 | struct zl6100_data *data = to_zl6100_data(info); | |
67 | int ret; | |
68 | ||
69 | if (page || reg >= PMBUS_VIRT_BASE) | |
70 | return -ENXIO; | |
71 | ||
bc581e6f GR |
72 | if (data->id == zl2005) { |
73 | /* | |
74 | * Limit register detection is not reliable on ZL2005. | |
75 | * Make sure registers are not erroneously detected. | |
76 | */ | |
77 | switch (reg) { | |
78 | case PMBUS_VOUT_OV_WARN_LIMIT: | |
79 | case PMBUS_VOUT_UV_WARN_LIMIT: | |
80 | case PMBUS_IOUT_OC_WARN_LIMIT: | |
81 | return -ENXIO; | |
82 | } | |
83 | } | |
84 | ||
200855e5 GR |
85 | zl6100_wait(data); |
86 | ret = pmbus_read_word_data(client, page, reg); | |
87 | data->access = ktime_get(); | |
88 | ||
89 | return ret; | |
90 | } | |
91 | ||
92 | static int zl6100_read_byte_data(struct i2c_client *client, int page, int reg) | |
93 | { | |
94 | const struct pmbus_driver_info *info = pmbus_get_driver_info(client); | |
95 | struct zl6100_data *data = to_zl6100_data(info); | |
96 | int ret; | |
97 | ||
98 | if (page > 0) | |
99 | return -ENXIO; | |
100 | ||
101 | zl6100_wait(data); | |
102 | ret = pmbus_read_byte_data(client, page, reg); | |
103 | data->access = ktime_get(); | |
104 | ||
105 | return ret; | |
106 | } | |
107 | ||
108 | static int zl6100_write_word_data(struct i2c_client *client, int page, int reg, | |
109 | u16 word) | |
110 | { | |
111 | const struct pmbus_driver_info *info = pmbus_get_driver_info(client); | |
112 | struct zl6100_data *data = to_zl6100_data(info); | |
113 | int ret; | |
114 | ||
115 | if (page || reg >= PMBUS_VIRT_BASE) | |
116 | return -ENXIO; | |
117 | ||
118 | zl6100_wait(data); | |
119 | ret = pmbus_write_word_data(client, page, reg, word); | |
120 | data->access = ktime_get(); | |
121 | ||
122 | return ret; | |
123 | } | |
124 | ||
125 | static int zl6100_write_byte(struct i2c_client *client, int page, u8 value) | |
126 | { | |
127 | const struct pmbus_driver_info *info = pmbus_get_driver_info(client); | |
128 | struct zl6100_data *data = to_zl6100_data(info); | |
129 | int ret; | |
130 | ||
131 | if (page > 0) | |
132 | return -ENXIO; | |
133 | ||
134 | zl6100_wait(data); | |
135 | ret = pmbus_write_byte(client, page, value); | |
136 | data->access = ktime_get(); | |
137 | ||
138 | return ret; | |
139 | } | |
140 | ||
141 | static const struct i2c_device_id zl6100_id[] = { | |
443830f6 GR |
142 | {"bmr450", zl2005}, |
143 | {"bmr451", zl2005}, | |
144 | {"bmr462", zl2008}, | |
145 | {"bmr463", zl2008}, | |
146 | {"bmr464", zl2008}, | |
200855e5 | 147 | {"zl2004", zl2004}, |
bc581e6f | 148 | {"zl2005", zl2005}, |
200855e5 GR |
149 | {"zl2006", zl2006}, |
150 | {"zl2008", zl2008}, | |
151 | {"zl2105", zl2105}, | |
152 | {"zl2106", zl2106}, | |
153 | {"zl6100", zl6100}, | |
154 | {"zl6105", zl6105}, | |
155 | { } | |
156 | }; | |
157 | MODULE_DEVICE_TABLE(i2c, zl6100_id); | |
158 | ||
159 | static int zl6100_probe(struct i2c_client *client, | |
160 | const struct i2c_device_id *id) | |
161 | { | |
162 | int ret; | |
163 | struct zl6100_data *data; | |
164 | struct pmbus_driver_info *info; | |
165 | u8 device_id[I2C_SMBUS_BLOCK_MAX + 1]; | |
166 | const struct i2c_device_id *mid; | |
167 | ||
168 | if (!i2c_check_functionality(client->adapter, | |
56badacb | 169 | I2C_FUNC_SMBUS_READ_WORD_DATA |
200855e5 GR |
170 | | I2C_FUNC_SMBUS_READ_BLOCK_DATA)) |
171 | return -ENODEV; | |
172 | ||
173 | ret = i2c_smbus_read_block_data(client, ZL6100_DEVICE_ID, | |
174 | device_id); | |
175 | if (ret < 0) { | |
176 | dev_err(&client->dev, "Failed to read device ID\n"); | |
177 | return ret; | |
178 | } | |
179 | device_id[ret] = '\0'; | |
180 | dev_info(&client->dev, "Device ID %s\n", device_id); | |
181 | ||
182 | mid = NULL; | |
183 | for (mid = zl6100_id; mid->name[0]; mid++) { | |
184 | if (!strncasecmp(mid->name, device_id, strlen(mid->name))) | |
185 | break; | |
186 | } | |
187 | if (!mid->name[0]) { | |
188 | dev_err(&client->dev, "Unsupported device\n"); | |
189 | return -ENODEV; | |
190 | } | |
191 | if (id->driver_data != mid->driver_data) | |
192 | dev_notice(&client->dev, | |
193 | "Device mismatch: Configured %s, detected %s\n", | |
194 | id->name, mid->name); | |
195 | ||
196 | data = kzalloc(sizeof(struct zl6100_data), GFP_KERNEL); | |
197 | if (!data) | |
198 | return -ENOMEM; | |
199 | ||
200 | data->id = mid->driver_data; | |
201 | ||
202 | /* | |
fecfb644 GR |
203 | * According to information from the chip vendor, all currently |
204 | * supported chips are known to require a wait time between I2C | |
205 | * accesses. | |
200855e5 | 206 | */ |
7ad6307a | 207 | data->delay = delay; |
200855e5 GR |
208 | |
209 | /* | |
210 | * Since there was a direct I2C device access above, wait before | |
211 | * accessing the chip again. | |
200855e5 GR |
212 | */ |
213 | data->access = ktime_get(); | |
214 | zl6100_wait(data); | |
200855e5 GR |
215 | |
216 | info = &data->info; | |
217 | ||
218 | info->pages = 1; | |
219 | info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT | |
220 | | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT | |
221 | | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT | |
56badacb GR |
222 | | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP; |
223 | ||
224 | ret = i2c_smbus_read_word_data(client, ZL6100_MFR_CONFIG); | |
225 | if (ret < 0) | |
226 | goto err_mem; | |
227 | if (ret & ZL6100_MFR_XTEMP_ENABLE) | |
228 | info->func[0] |= PMBUS_HAVE_TEMP2; | |
229 | ||
230 | data->access = ktime_get(); | |
231 | zl6100_wait(data); | |
200855e5 GR |
232 | |
233 | info->read_word_data = zl6100_read_word_data; | |
234 | info->read_byte_data = zl6100_read_byte_data; | |
235 | info->write_word_data = zl6100_write_word_data; | |
236 | info->write_byte = zl6100_write_byte; | |
237 | ||
238 | ret = pmbus_do_probe(client, mid, info); | |
239 | if (ret) | |
240 | goto err_mem; | |
241 | return 0; | |
242 | ||
243 | err_mem: | |
244 | kfree(data); | |
245 | return ret; | |
246 | } | |
247 | ||
248 | static int zl6100_remove(struct i2c_client *client) | |
249 | { | |
250 | const struct pmbus_driver_info *info = pmbus_get_driver_info(client); | |
251 | const struct zl6100_data *data = to_zl6100_data(info); | |
252 | ||
253 | pmbus_do_remove(client); | |
254 | kfree(data); | |
255 | return 0; | |
256 | } | |
257 | ||
258 | static struct i2c_driver zl6100_driver = { | |
259 | .driver = { | |
260 | .name = "zl6100", | |
261 | }, | |
262 | .probe = zl6100_probe, | |
263 | .remove = zl6100_remove, | |
264 | .id_table = zl6100_id, | |
265 | }; | |
266 | ||
f0967eea | 267 | module_i2c_driver(zl6100_driver); |
200855e5 GR |
268 | |
269 | MODULE_AUTHOR("Guenter Roeck"); | |
270 | MODULE_DESCRIPTION("PMBus driver for ZL6100 and compatibles"); | |
271 | MODULE_LICENSE("GPL"); |