blob: 5489d70015c25d1949e7b40c2a69b2749703839b [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) {
126 case PMBUS_IIN_OC_WARN_LIMIT:
127 ret = pmbus_write_word_data(client, 0,
128 LM25066_MFR_IIN_OC_WARN_LIMIT,
129 word);
130 break;
131 case PMBUS_PIN_OP_WARN_LIMIT:
132 ret = pmbus_write_word_data(client, 0,
133 LM25066_MFR_PIN_OP_WARN_LIMIT,
134 word);
135 break;
136 case PMBUS_VIRT_RESET_PIN_HISTORY:
137 ret = pmbus_write_byte(client, 0, LM25066_CLEAR_PIN_PEAK);
138 break;
139 default:
140 ret = -ENODATA;
141 break;
142 }
143 return ret;
144}
145
146static int lm25066_probe(struct i2c_client *client,
147 const struct i2c_device_id *id)
148{
149 int config;
Guenter Roeck03e9bd82011-07-08 10:43:57 -0700150 struct lm25066_data *data;
151 struct pmbus_driver_info *info;
152
153 if (!i2c_check_functionality(client->adapter,
154 I2C_FUNC_SMBUS_READ_BYTE_DATA))
155 return -ENODEV;
156
Guenter Roeck8b313ca2012-02-22 08:56:43 -0800157 data = devm_kzalloc(&client->dev, sizeof(struct lm25066_data),
158 GFP_KERNEL);
Guenter Roeck03e9bd82011-07-08 10:43:57 -0700159 if (!data)
160 return -ENOMEM;
161
162 config = i2c_smbus_read_byte_data(client, LM25066_DEVICE_SETUP);
Guenter Roeck8b313ca2012-02-22 08:56:43 -0800163 if (config < 0)
164 return config;
Guenter Roeck03e9bd82011-07-08 10:43:57 -0700165
166 data->id = id->driver_data;
167 info = &data->info;
168
Guenter Roecka7c69112013-02-06 09:55:37 -0800169 info->pages = 1;
Guenter Roeck03e9bd82011-07-08 10:43:57 -0700170 info->format[PSC_VOLTAGE_IN] = direct;
171 info->format[PSC_VOLTAGE_OUT] = direct;
172 info->format[PSC_CURRENT_IN] = direct;
173 info->format[PSC_TEMPERATURE] = direct;
174 info->format[PSC_POWER] = direct;
175
176 info->m[PSC_TEMPERATURE] = 16;
177 info->b[PSC_TEMPERATURE] = 0;
178 info->R[PSC_TEMPERATURE] = 0;
179
Guenter Roecka7c69112013-02-06 09:55:37 -0800180 info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VMON | PMBUS_HAVE_VOUT
Guenter Roeck03e9bd82011-07-08 10:43:57 -0700181 | PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_PIN | PMBUS_HAVE_IIN
182 | PMBUS_HAVE_STATUS_INPUT | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
Guenter Roeck03e9bd82011-07-08 10:43:57 -0700183
184 info->read_word_data = lm25066_read_word_data;
185 info->write_word_data = lm25066_write_word_data;
Guenter Roeck03e9bd82011-07-08 10:43:57 -0700186
187 switch (id->driver_data) {
188 case lm25066:
189 info->m[PSC_VOLTAGE_IN] = 22070;
190 info->b[PSC_VOLTAGE_IN] = 0;
191 info->R[PSC_VOLTAGE_IN] = -2;
192 info->m[PSC_VOLTAGE_OUT] = 22070;
193 info->b[PSC_VOLTAGE_OUT] = 0;
194 info->R[PSC_VOLTAGE_OUT] = -2;
195
196 if (config & LM25066_DEV_SETUP_CL) {
197 info->m[PSC_CURRENT_IN] = 6852;
198 info->b[PSC_CURRENT_IN] = 0;
199 info->R[PSC_CURRENT_IN] = -2;
200 info->m[PSC_POWER] = 369;
201 info->b[PSC_POWER] = 0;
202 info->R[PSC_POWER] = -2;
203 } else {
204 info->m[PSC_CURRENT_IN] = 13661;
205 info->b[PSC_CURRENT_IN] = 0;
206 info->R[PSC_CURRENT_IN] = -2;
207 info->m[PSC_POWER] = 736;
208 info->b[PSC_POWER] = 0;
209 info->R[PSC_POWER] = -2;
210 }
211 break;
212 case lm5064:
213 info->m[PSC_VOLTAGE_IN] = 22075;
214 info->b[PSC_VOLTAGE_IN] = 0;
215 info->R[PSC_VOLTAGE_IN] = -2;
216 info->m[PSC_VOLTAGE_OUT] = 22075;
217 info->b[PSC_VOLTAGE_OUT] = 0;
218 info->R[PSC_VOLTAGE_OUT] = -2;
219
220 if (config & LM25066_DEV_SETUP_CL) {
221 info->m[PSC_CURRENT_IN] = 6713;
222 info->b[PSC_CURRENT_IN] = 0;
223 info->R[PSC_CURRENT_IN] = -2;
224 info->m[PSC_POWER] = 3619;
225 info->b[PSC_POWER] = 0;
226 info->R[PSC_POWER] = -3;
227 } else {
228 info->m[PSC_CURRENT_IN] = 13426;
229 info->b[PSC_CURRENT_IN] = 0;
230 info->R[PSC_CURRENT_IN] = -2;
231 info->m[PSC_POWER] = 7238;
232 info->b[PSC_POWER] = 0;
233 info->R[PSC_POWER] = -3;
234 }
235 break;
236 case lm5066:
237 info->m[PSC_VOLTAGE_IN] = 4587;
238 info->b[PSC_VOLTAGE_IN] = 0;
239 info->R[PSC_VOLTAGE_IN] = -2;
240 info->m[PSC_VOLTAGE_OUT] = 4587;
241 info->b[PSC_VOLTAGE_OUT] = 0;
242 info->R[PSC_VOLTAGE_OUT] = -2;
243
244 if (config & LM25066_DEV_SETUP_CL) {
245 info->m[PSC_CURRENT_IN] = 10753;
246 info->b[PSC_CURRENT_IN] = 0;
247 info->R[PSC_CURRENT_IN] = -2;
248 info->m[PSC_POWER] = 1204;
249 info->b[PSC_POWER] = 0;
250 info->R[PSC_POWER] = -3;
251 } else {
252 info->m[PSC_CURRENT_IN] = 5405;
253 info->b[PSC_CURRENT_IN] = 0;
254 info->R[PSC_CURRENT_IN] = -2;
255 info->m[PSC_POWER] = 605;
256 info->b[PSC_POWER] = 0;
257 info->R[PSC_POWER] = -3;
258 }
259 break;
260 default:
Guenter Roeck8b313ca2012-02-22 08:56:43 -0800261 return -ENODEV;
Guenter Roeck03e9bd82011-07-08 10:43:57 -0700262 }
263
Guenter Roeck8b313ca2012-02-22 08:56:43 -0800264 return pmbus_do_probe(client, id, info);
Guenter Roeck03e9bd82011-07-08 10:43:57 -0700265}
266
Guenter Roeck03e9bd82011-07-08 10:43:57 -0700267static const struct i2c_device_id lm25066_id[] = {
268 {"lm25066", lm25066},
269 {"lm5064", lm5064},
270 {"lm5066", lm5066},
271 { }
272};
273
274MODULE_DEVICE_TABLE(i2c, lm25066_id);
275
276/* This is the driver that will be inserted */
277static struct i2c_driver lm25066_driver = {
278 .driver = {
279 .name = "lm25066",
280 },
281 .probe = lm25066_probe,
Guenter Roeckdd285ad2012-02-22 08:56:44 -0800282 .remove = pmbus_do_remove,
Guenter Roeck03e9bd82011-07-08 10:43:57 -0700283 .id_table = lm25066_id,
284};
285
Axel Linf0967ee2012-01-20 15:38:18 +0800286module_i2c_driver(lm25066_driver);
Guenter Roeck03e9bd82011-07-08 10:43:57 -0700287
288MODULE_AUTHOR("Guenter Roeck");
289MODULE_DESCRIPTION("PMBus driver for LM25066/LM5064/LM5066");
290MODULE_LICENSE("GPL");