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