hwmon: (pmbus/adm1275) Add support for second current limit
[deliverable/linux.git] / drivers / hwmon / pmbus / adm1275.c
CommitLineData
83f7649c
GR
1/*
2 * Hardware monitoring driver for Analog Devices ADM1275 Hot-Swap Controller
3 * and Digital Power Monitor
4 *
5 * Copyright (c) 2011 Ericsson AB.
6 *
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
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/init.h>
21#include <linux/err.h>
22#include <linux/slab.h>
23#include <linux/i2c.h>
24#include "pmbus.h"
25
c576e30c
GR
26#define ADM1275_PEAK_IOUT 0xd0
27#define ADM1275_PEAK_VIN 0xd1
28#define ADM1275_PEAK_VOUT 0xd2
83f7649c
GR
29#define ADM1275_PMON_CONFIG 0xd4
30
31#define ADM1275_VIN_VOUT_SELECT (1 << 6)
32#define ADM1275_VRANGE (1 << 5)
33
c5e67636
GR
34#define ADM1275_IOUT_WARN2_LIMIT 0xd7
35#define ADM1275_DEVICE_CONFIG 0xd8
36
37#define ADM1275_IOUT_WARN2_SELECT (1 << 4)
38
39#define ADM1275_MFR_STATUS_IOUT_WARN2 (1 << 0)
40
41struct adm1275_data {
42 bool have_oc_fault;
43 struct pmbus_driver_info info;
44};
45
46#define to_adm1275_data(x) container_of(x, struct adm1275_data, info)
47
c576e30c
GR
48static int adm1275_read_word_data(struct i2c_client *client, int page, int reg)
49{
c5e67636
GR
50 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
51 const struct adm1275_data *data = to_adm1275_data(info);
c576e30c
GR
52 int ret;
53
54 if (page)
c5e67636 55 return -ENXIO;
c576e30c
GR
56
57 switch (reg) {
c5e67636
GR
58 case PMBUS_IOUT_UC_FAULT_LIMIT:
59 if (data->have_oc_fault) {
60 ret = -ENXIO;
61 break;
62 }
63 ret = pmbus_read_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT);
64 break;
65 case PMBUS_IOUT_OC_FAULT_LIMIT:
66 if (!data->have_oc_fault) {
67 ret = -ENXIO;
68 break;
69 }
70 ret = pmbus_read_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT);
71 break;
c576e30c
GR
72 case PMBUS_VIRT_READ_IOUT_MAX:
73 ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_IOUT);
74 break;
75 case PMBUS_VIRT_READ_VOUT_MAX:
76 ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_VOUT);
77 break;
78 case PMBUS_VIRT_READ_VIN_MAX:
79 ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_VIN);
80 break;
81 case PMBUS_VIRT_RESET_IOUT_HISTORY:
82 case PMBUS_VIRT_RESET_VOUT_HISTORY:
83 case PMBUS_VIRT_RESET_VIN_HISTORY:
84 ret = 0;
85 break;
86 default:
87 ret = -ENODATA;
88 break;
89 }
90 return ret;
91}
92
93static int adm1275_write_word_data(struct i2c_client *client, int page, int reg,
94 u16 word)
95{
96 int ret;
97
98 if (page)
c5e67636 99 return -ENXIO;
c576e30c
GR
100
101 switch (reg) {
c5e67636
GR
102 case PMBUS_IOUT_UC_FAULT_LIMIT:
103 case PMBUS_IOUT_OC_FAULT_LIMIT:
104 ret = pmbus_write_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT,
105 word);
106 break;
c576e30c
GR
107 case PMBUS_VIRT_RESET_IOUT_HISTORY:
108 ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_IOUT, 0);
109 break;
110 case PMBUS_VIRT_RESET_VOUT_HISTORY:
111 ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VOUT, 0);
112 break;
113 case PMBUS_VIRT_RESET_VIN_HISTORY:
114 ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VIN, 0);
115 break;
116 default:
117 ret = -ENODATA;
118 break;
119 }
120 return ret;
121}
122
c5e67636
GR
123static int adm1275_read_byte_data(struct i2c_client *client, int page, int reg)
124{
125 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
126 const struct adm1275_data *data = to_adm1275_data(info);
127 int mfr_status, ret;
128
129 if (page)
130 return -ENXIO;
131
132 switch (reg) {
133 case PMBUS_STATUS_IOUT:
134 ret = pmbus_read_byte_data(client, page, PMBUS_STATUS_IOUT);
135 if (ret < 0)
136 break;
137 mfr_status = pmbus_read_byte_data(client, page,
138 PMBUS_STATUS_MFR_SPECIFIC);
139 if (mfr_status < 0) {
140 ret = mfr_status;
141 break;
142 }
143 if (mfr_status & ADM1275_MFR_STATUS_IOUT_WARN2) {
144 ret |= data->have_oc_fault ?
145 PB_IOUT_OC_FAULT : PB_IOUT_UC_FAULT;
146 }
147 break;
148 default:
149 ret = -ENODATA;
150 break;
151 }
152 return ret;
153}
154
83f7649c
GR
155static int adm1275_probe(struct i2c_client *client,
156 const struct i2c_device_id *id)
157{
c5e67636 158 int config, device_config;
3b33ca41 159 int ret;
83f7649c 160 struct pmbus_driver_info *info;
c5e67636 161 struct adm1275_data *data;
83f7649c
GR
162
163 if (!i2c_check_functionality(client->adapter,
164 I2C_FUNC_SMBUS_READ_BYTE_DATA))
165 return -ENODEV;
166
c5e67636
GR
167 data = kzalloc(sizeof(struct adm1275_data), GFP_KERNEL);
168 if (!data)
83f7649c
GR
169 return -ENOMEM;
170
171 config = i2c_smbus_read_byte_data(client, ADM1275_PMON_CONFIG);
3b33ca41
GR
172 if (config < 0) {
173 ret = config;
174 goto err_mem;
175 }
83f7649c 176
c5e67636
GR
177 device_config = i2c_smbus_read_byte_data(client, ADM1275_DEVICE_CONFIG);
178 if (device_config < 0) {
179 ret = device_config;
180 goto err_mem;
181 }
182
183 info = &data->info;
184
83f7649c 185 info->pages = 1;
1061d851
GR
186 info->format[PSC_VOLTAGE_IN] = direct;
187 info->format[PSC_VOLTAGE_OUT] = direct;
188 info->format[PSC_CURRENT_OUT] = direct;
7e97bbba 189 info->m[PSC_CURRENT_OUT] = 807;
83f7649c
GR
190 info->b[PSC_CURRENT_OUT] = 20475;
191 info->R[PSC_CURRENT_OUT] = -1;
192 info->func[0] = PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT;
193
c576e30c 194 info->read_word_data = adm1275_read_word_data;
c5e67636 195 info->read_byte_data = adm1275_read_byte_data;
c576e30c
GR
196 info->write_word_data = adm1275_write_word_data;
197
83f7649c 198 if (config & ADM1275_VRANGE) {
7e97bbba 199 info->m[PSC_VOLTAGE_IN] = 19199;
83f7649c
GR
200 info->b[PSC_VOLTAGE_IN] = 0;
201 info->R[PSC_VOLTAGE_IN] = -2;
7e97bbba 202 info->m[PSC_VOLTAGE_OUT] = 19199;
83f7649c
GR
203 info->b[PSC_VOLTAGE_OUT] = 0;
204 info->R[PSC_VOLTAGE_OUT] = -2;
205 } else {
7e97bbba 206 info->m[PSC_VOLTAGE_IN] = 6720;
83f7649c
GR
207 info->b[PSC_VOLTAGE_IN] = 0;
208 info->R[PSC_VOLTAGE_IN] = -1;
7e97bbba 209 info->m[PSC_VOLTAGE_OUT] = 6720;
83f7649c
GR
210 info->b[PSC_VOLTAGE_OUT] = 0;
211 info->R[PSC_VOLTAGE_OUT] = -1;
212 }
213
c5e67636
GR
214 if (device_config & ADM1275_IOUT_WARN2_SELECT)
215 data->have_oc_fault = true;
216
83f7649c
GR
217 if (config & ADM1275_VIN_VOUT_SELECT)
218 info->func[0] |= PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
219 else
220 info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT;
221
3b33ca41
GR
222 ret = pmbus_do_probe(client, id, info);
223 if (ret)
224 goto err_mem;
225 return 0;
226
227err_mem:
c5e67636 228 kfree(data);
3b33ca41 229 return ret;
83f7649c
GR
230}
231
232static int adm1275_remove(struct i2c_client *client)
233{
234 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
c5e67636 235 const struct adm1275_data *data = to_adm1275_data(info);
83f7649c 236
866cf12a 237 pmbus_do_remove(client);
c5e67636 238 kfree(data);
866cf12a 239 return 0;
83f7649c
GR
240}
241
242static const struct i2c_device_id adm1275_id[] = {
243 {"adm1275", 0},
244 { }
245};
246MODULE_DEVICE_TABLE(i2c, adm1275_id);
247
248static struct i2c_driver adm1275_driver = {
249 .driver = {
250 .name = "adm1275",
251 },
252 .probe = adm1275_probe,
253 .remove = adm1275_remove,
254 .id_table = adm1275_id,
255};
256
257static int __init adm1275_init(void)
258{
259 return i2c_add_driver(&adm1275_driver);
260}
261
262static void __exit adm1275_exit(void)
263{
264 i2c_del_driver(&adm1275_driver);
265}
266
267MODULE_AUTHOR("Guenter Roeck");
268MODULE_DESCRIPTION("PMBus driver for Analog Devices ADM1275");
269MODULE_LICENSE("GPL");
270module_init(adm1275_init);
271module_exit(adm1275_exit);
This page took 0.078227 seconds and 5 git commands to generate.