blob: f9f0c93e4a3d979088449a60241126d748d05996 [file] [log] [blame]
Guenter Roeck03e9bd82011-07-08 10:43:57 -07001/*
2 * Hardware monitoring driver for LM25066 / LM5064 / LM5066
3 *
4 * Copyright (c) 2011 Ericsson AB.
Guenter Roecka7c69112013-02-06 09:55:37 -08005 * Copyright (c) 2013 Guenter Roeck
Guenter Roeck03e9bd82011-07-08 10:43:57 -07006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/init.h>
25#include <linux/err.h>
26#include <linux/slab.h>
27#include <linux/i2c.h>
28#include "pmbus.h"
29
30enum chips { lm25066, lm5064, lm5066 };
31
32#define LM25066_READ_VAUX 0xd0
33#define LM25066_MFR_READ_IIN 0xd1
34#define LM25066_MFR_READ_PIN 0xd2
35#define LM25066_MFR_IIN_OC_WARN_LIMIT 0xd3
36#define LM25066_MFR_PIN_OP_WARN_LIMIT 0xd4
37#define LM25066_READ_PIN_PEAK 0xd5
38#define LM25066_CLEAR_PIN_PEAK 0xd6
39#define LM25066_DEVICE_SETUP 0xd9
40#define LM25066_READ_AVG_VIN 0xdc
41#define LM25066_READ_AVG_VOUT 0xdd
42#define LM25066_READ_AVG_IIN 0xde
43#define LM25066_READ_AVG_PIN 0xdf
44
45#define LM25066_DEV_SETUP_CL (1 << 4) /* Current limit */
46
47struct lm25066_data {
48 int id;
49 struct pmbus_driver_info info;
50};
51
52#define to_lm25066_data(x) container_of(x, struct lm25066_data, info)
53
54static int lm25066_read_word_data(struct i2c_client *client, int page, int reg)
55{
56 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
57 const struct lm25066_data *data = to_lm25066_data(info);
58 int ret;
59
Guenter Roecka7c69112013-02-06 09:55:37 -080060 switch (reg) {
61 case PMBUS_VIRT_READ_VMON:
62 ret = pmbus_read_word_data(client, 0, LM25066_READ_VAUX);
63 if (ret < 0)
Guenter Roeck03e9bd82011-07-08 10:43:57 -070064 break;
Guenter Roecka7c69112013-02-06 09:55:37 -080065 /* Adjust returned value to match VIN coefficients */
66 switch (data->id) {
67 case lm25066:
68 /* VIN: 4.54 mV VAUX: 283.2 uV LSB */
69 ret = DIV_ROUND_CLOSEST(ret * 2832, 45400);
70 break;
71 case lm5064:
72 /* VIN: 4.53 mV VAUX: 700 uV LSB */
73 ret = DIV_ROUND_CLOSEST(ret * 70, 453);
74 break;
75 case lm5066:
76 /* VIN: 2.18 mV VAUX: 725 uV LSB */
77 ret = DIV_ROUND_CLOSEST(ret * 725, 2180);
Guenter Roeck03e9bd82011-07-08 10:43:57 -070078 break;
79 }
Guenter Roecka7c69112013-02-06 09:55:37 -080080 break;
Guenter Roeck03e9bd82011-07-08 10:43:57 -070081 case PMBUS_READ_IIN:
82 ret = pmbus_read_word_data(client, 0, LM25066_MFR_READ_IIN);
83 break;
84 case PMBUS_READ_PIN:
85 ret = pmbus_read_word_data(client, 0, LM25066_MFR_READ_PIN);
86 break;
87 case PMBUS_IIN_OC_WARN_LIMIT:
88 ret = pmbus_read_word_data(client, 0,
89 LM25066_MFR_IIN_OC_WARN_LIMIT);
90 break;
91 case PMBUS_PIN_OP_WARN_LIMIT:
92 ret = pmbus_read_word_data(client, 0,
93 LM25066_MFR_PIN_OP_WARN_LIMIT);
94 break;
95 case PMBUS_VIRT_READ_VIN_AVG:
96 ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_VIN);
97 break;
98 case PMBUS_VIRT_READ_VOUT_AVG:
99 ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_VOUT);
100 break;
101 case PMBUS_VIRT_READ_IIN_AVG:
102 ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_IIN);
103 break;
104 case PMBUS_VIRT_READ_PIN_AVG:
105 ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_PIN);
106 break;
107 case PMBUS_VIRT_READ_PIN_MAX:
108 ret = pmbus_read_word_data(client, 0, LM25066_READ_PIN_PEAK);
109 break;
110 case PMBUS_VIRT_RESET_PIN_HISTORY:
111 ret = 0;
112 break;
113 default:
114 ret = -ENODATA;
115 break;
116 }
Guenter Roeck03e9bd82011-07-08 10:43:57 -0700117 return ret;
118}
119
120static int lm25066_write_word_data(struct i2c_client *client, int page, int reg,
121 u16 word)
122{
123 int ret;
124
Guenter Roeck03e9bd82011-07-08 10:43:57 -0700125 switch (reg) {
Guenter Roeck2507abb2013-02-06 20:49:12 -0800126 case PMBUS_VOUT_UV_WARN_LIMIT:
127 case PMBUS_OT_FAULT_LIMIT:
128 case PMBUS_OT_WARN_LIMIT:
129 case PMBUS_VIN_UV_WARN_LIMIT:
130 case PMBUS_VIN_OV_WARN_LIMIT:
131 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, 0x0fff);
132 ret = pmbus_write_word_data(client, 0, reg, word);
133 pmbus_clear_cache(client);
134 break;
Guenter Roeck03e9bd82011-07-08 10:43:57 -0700135 case PMBUS_IIN_OC_WARN_LIMIT:
Guenter Roeck2507abb2013-02-06 20:49:12 -0800136 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, 0x0fff);
Guenter Roeck03e9bd82011-07-08 10:43:57 -0700137 ret = pmbus_write_word_data(client, 0,
138 LM25066_MFR_IIN_OC_WARN_LIMIT,
139 word);
Guenter Roeck2507abb2013-02-06 20:49:12 -0800140 pmbus_clear_cache(client);
Guenter Roeck03e9bd82011-07-08 10:43:57 -0700141 break;
142 case PMBUS_PIN_OP_WARN_LIMIT:
Guenter Roeck2507abb2013-02-06 20:49:12 -0800143 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, 0x0fff);
Guenter Roeck03e9bd82011-07-08 10:43:57 -0700144 ret = pmbus_write_word_data(client, 0,
145 LM25066_MFR_PIN_OP_WARN_LIMIT,
146 word);
Guenter Roeck2507abb2013-02-06 20:49:12 -0800147 pmbus_clear_cache(client);
Guenter Roeck03e9bd82011-07-08 10:43:57 -0700148 break;
149 case PMBUS_VIRT_RESET_PIN_HISTORY:
150 ret = pmbus_write_byte(client, 0, LM25066_CLEAR_PIN_PEAK);
151 break;
152 default:
153 ret = -ENODATA;
154 break;
155 }
156 return ret;
157}
158
159static int lm25066_probe(struct i2c_client *client,
160 const struct i2c_device_id *id)
161{
162 int config;
Guenter Roeck03e9bd82011-07-08 10:43:57 -0700163 struct lm25066_data *data;
164 struct pmbus_driver_info *info;
165
166 if (!i2c_check_functionality(client->adapter,
167 I2C_FUNC_SMBUS_READ_BYTE_DATA))
168 return -ENODEV;
169
Guenter Roeck8b313ca2012-02-22 08:56:43 -0800170 data = devm_kzalloc(&client->dev, sizeof(struct lm25066_data),
171 GFP_KERNEL);
Guenter Roeck03e9bd82011-07-08 10:43:57 -0700172 if (!data)
173 return -ENOMEM;
174
175 config = i2c_smbus_read_byte_data(client, LM25066_DEVICE_SETUP);
Guenter Roeck8b313ca2012-02-22 08:56:43 -0800176 if (config < 0)
177 return config;
Guenter Roeck03e9bd82011-07-08 10:43:57 -0700178
179 data->id = id->driver_data;
180 info = &data->info;
181
Guenter Roecka7c69112013-02-06 09:55:37 -0800182 info->pages = 1;
Guenter Roeck03e9bd82011-07-08 10:43:57 -0700183 info->format[PSC_VOLTAGE_IN] = direct;
184 info->format[PSC_VOLTAGE_OUT] = direct;
185 info->format[PSC_CURRENT_IN] = direct;
186 info->format[PSC_TEMPERATURE] = direct;
187 info->format[PSC_POWER] = direct;
188
189 info->m[PSC_TEMPERATURE] = 16;
190 info->b[PSC_TEMPERATURE] = 0;
191 info->R[PSC_TEMPERATURE] = 0;
192
Guenter Roecka7c69112013-02-06 09:55:37 -0800193 info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VMON | PMBUS_HAVE_VOUT
Guenter Roeck03e9bd82011-07-08 10:43:57 -0700194 | PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_PIN | PMBUS_HAVE_IIN
195 | PMBUS_HAVE_STATUS_INPUT | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
Guenter Roeck03e9bd82011-07-08 10:43:57 -0700196
197 info->read_word_data = lm25066_read_word_data;
198 info->write_word_data = lm25066_write_word_data;
Guenter Roeck03e9bd82011-07-08 10:43:57 -0700199
200 switch (id->driver_data) {
201 case lm25066:
202 info->m[PSC_VOLTAGE_IN] = 22070;
203 info->b[PSC_VOLTAGE_IN] = 0;
204 info->R[PSC_VOLTAGE_IN] = -2;
205 info->m[PSC_VOLTAGE_OUT] = 22070;
206 info->b[PSC_VOLTAGE_OUT] = 0;
207 info->R[PSC_VOLTAGE_OUT] = -2;
208
209 if (config & LM25066_DEV_SETUP_CL) {
210 info->m[PSC_CURRENT_IN] = 6852;
211 info->b[PSC_CURRENT_IN] = 0;
212 info->R[PSC_CURRENT_IN] = -2;
213 info->m[PSC_POWER] = 369;
214 info->b[PSC_POWER] = 0;
215 info->R[PSC_POWER] = -2;
216 } else {
217 info->m[PSC_CURRENT_IN] = 13661;
218 info->b[PSC_CURRENT_IN] = 0;
219 info->R[PSC_CURRENT_IN] = -2;
220 info->m[PSC_POWER] = 736;
221 info->b[PSC_POWER] = 0;
222 info->R[PSC_POWER] = -2;
223 }
224 break;
225 case lm5064:
226 info->m[PSC_VOLTAGE_IN] = 22075;
227 info->b[PSC_VOLTAGE_IN] = 0;
228 info->R[PSC_VOLTAGE_IN] = -2;
229 info->m[PSC_VOLTAGE_OUT] = 22075;
230 info->b[PSC_VOLTAGE_OUT] = 0;
231 info->R[PSC_VOLTAGE_OUT] = -2;
232
233 if (config & LM25066_DEV_SETUP_CL) {
234 info->m[PSC_CURRENT_IN] = 6713;
235 info->b[PSC_CURRENT_IN] = 0;
236 info->R[PSC_CURRENT_IN] = -2;
237 info->m[PSC_POWER] = 3619;
238 info->b[PSC_POWER] = 0;
239 info->R[PSC_POWER] = -3;
240 } else {
241 info->m[PSC_CURRENT_IN] = 13426;
242 info->b[PSC_CURRENT_IN] = 0;
243 info->R[PSC_CURRENT_IN] = -2;
244 info->m[PSC_POWER] = 7238;
245 info->b[PSC_POWER] = 0;
246 info->R[PSC_POWER] = -3;
247 }
248 break;
249 case lm5066:
250 info->m[PSC_VOLTAGE_IN] = 4587;
251 info->b[PSC_VOLTAGE_IN] = 0;
252 info->R[PSC_VOLTAGE_IN] = -2;
253 info->m[PSC_VOLTAGE_OUT] = 4587;
254 info->b[PSC_VOLTAGE_OUT] = 0;
255 info->R[PSC_VOLTAGE_OUT] = -2;
256
257 if (config & LM25066_DEV_SETUP_CL) {
258 info->m[PSC_CURRENT_IN] = 10753;
259 info->b[PSC_CURRENT_IN] = 0;
260 info->R[PSC_CURRENT_IN] = -2;
261 info->m[PSC_POWER] = 1204;
262 info->b[PSC_POWER] = 0;
263 info->R[PSC_POWER] = -3;
264 } else {
265 info->m[PSC_CURRENT_IN] = 5405;
266 info->b[PSC_CURRENT_IN] = 0;
267 info->R[PSC_CURRENT_IN] = -2;
268 info->m[PSC_POWER] = 605;
269 info->b[PSC_POWER] = 0;
270 info->R[PSC_POWER] = -3;
271 }
272 break;
273 default:
Guenter Roeck8b313ca2012-02-22 08:56:43 -0800274 return -ENODEV;
Guenter Roeck03e9bd82011-07-08 10:43:57 -0700275 }
276
Guenter Roeck8b313ca2012-02-22 08:56:43 -0800277 return pmbus_do_probe(client, id, info);
Guenter Roeck03e9bd82011-07-08 10:43:57 -0700278}
279
Guenter Roeck03e9bd82011-07-08 10:43:57 -0700280static const struct i2c_device_id lm25066_id[] = {
281 {"lm25066", lm25066},
282 {"lm5064", lm5064},
283 {"lm5066", lm5066},
284 { }
285};
286
287MODULE_DEVICE_TABLE(i2c, lm25066_id);
288
289/* This is the driver that will be inserted */
290static struct i2c_driver lm25066_driver = {
291 .driver = {
292 .name = "lm25066",
293 },
294 .probe = lm25066_probe,
Guenter Roeckdd285ad2012-02-22 08:56:44 -0800295 .remove = pmbus_do_remove,
Guenter Roeck03e9bd82011-07-08 10:43:57 -0700296 .id_table = lm25066_id,
297};
298
Axel Linf0967ee2012-01-20 15:38:18 +0800299module_i2c_driver(lm25066_driver);
Guenter Roeck03e9bd82011-07-08 10:43:57 -0700300
301MODULE_AUTHOR("Guenter Roeck");
302MODULE_DESCRIPTION("PMBus driver for LM25066/LM5064/LM5066");
303MODULE_LICENSE("GPL");