MIPS: Yosemite, Emma: Fix off-by-two in arcs_cmdline buffer size check
[linux-2.6/linux-mips.git] / drivers / hwmon / pmbus / pmbus.c
blob995e873197e3d4056b52a2d8efc8df2da7b8ee55
1 /*
2 * Hardware monitoring driver for PMBus devices
4 * Copyright (c) 2010, 2011 Ericsson AB.
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.
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.
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.
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/mutex.h>
27 #include <linux/i2c.h>
28 #include "pmbus.h"
31 * Find sensor groups and status registers on each page.
33 static void pmbus_find_sensor_groups(struct i2c_client *client,
34 struct pmbus_driver_info *info)
36 int page;
38 /* Sensors detected on page 0 only */
39 if (pmbus_check_word_register(client, 0, PMBUS_READ_VIN))
40 info->func[0] |= PMBUS_HAVE_VIN;
41 if (pmbus_check_word_register(client, 0, PMBUS_READ_VCAP))
42 info->func[0] |= PMBUS_HAVE_VCAP;
43 if (pmbus_check_word_register(client, 0, PMBUS_READ_IIN))
44 info->func[0] |= PMBUS_HAVE_IIN;
45 if (pmbus_check_word_register(client, 0, PMBUS_READ_PIN))
46 info->func[0] |= PMBUS_HAVE_PIN;
47 if (info->func[0]
48 && pmbus_check_byte_register(client, 0, PMBUS_STATUS_INPUT))
49 info->func[0] |= PMBUS_HAVE_STATUS_INPUT;
50 if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_12) &&
51 pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_1)) {
52 info->func[0] |= PMBUS_HAVE_FAN12;
53 if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_12))
54 info->func[0] |= PMBUS_HAVE_STATUS_FAN12;
56 if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_34) &&
57 pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_3)) {
58 info->func[0] |= PMBUS_HAVE_FAN34;
59 if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_34))
60 info->func[0] |= PMBUS_HAVE_STATUS_FAN34;
62 if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_1))
63 info->func[0] |= PMBUS_HAVE_TEMP;
64 if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_2))
65 info->func[0] |= PMBUS_HAVE_TEMP2;
66 if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_3))
67 info->func[0] |= PMBUS_HAVE_TEMP3;
68 if (info->func[0] & (PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2
69 | PMBUS_HAVE_TEMP3)
70 && pmbus_check_byte_register(client, 0,
71 PMBUS_STATUS_TEMPERATURE))
72 info->func[0] |= PMBUS_HAVE_STATUS_TEMP;
74 /* Sensors detected on all pages */
75 for (page = 0; page < info->pages; page++) {
76 if (pmbus_check_word_register(client, page, PMBUS_READ_VOUT)) {
77 info->func[page] |= PMBUS_HAVE_VOUT;
78 if (pmbus_check_byte_register(client, page,
79 PMBUS_STATUS_VOUT))
80 info->func[page] |= PMBUS_HAVE_STATUS_VOUT;
82 if (pmbus_check_word_register(client, page, PMBUS_READ_IOUT)) {
83 info->func[page] |= PMBUS_HAVE_IOUT;
84 if (pmbus_check_byte_register(client, 0,
85 PMBUS_STATUS_IOUT))
86 info->func[page] |= PMBUS_HAVE_STATUS_IOUT;
88 if (pmbus_check_word_register(client, page, PMBUS_READ_POUT))
89 info->func[page] |= PMBUS_HAVE_POUT;
94 * Identify chip parameters.
96 static int pmbus_identify(struct i2c_client *client,
97 struct pmbus_driver_info *info)
99 int ret = 0;
101 if (!info->pages) {
103 * Check if the PAGE command is supported. If it is,
104 * keep setting the page number until it fails or until the
105 * maximum number of pages has been reached. Assume that
106 * this is the number of pages supported by the chip.
108 if (pmbus_check_byte_register(client, 0, PMBUS_PAGE)) {
109 int page;
111 for (page = 1; page < PMBUS_PAGES; page++) {
112 if (pmbus_set_page(client, page) < 0)
113 break;
115 pmbus_set_page(client, 0);
116 info->pages = page;
117 } else {
118 info->pages = 1;
122 if (pmbus_check_byte_register(client, 0, PMBUS_VOUT_MODE)) {
123 int vout_mode;
125 vout_mode = pmbus_read_byte_data(client, 0, PMBUS_VOUT_MODE);
126 if (vout_mode >= 0 && vout_mode != 0xff) {
127 switch (vout_mode >> 5) {
128 case 0:
129 break;
130 case 1:
131 info->format[PSC_VOLTAGE_OUT] = vid;
132 break;
133 case 2:
134 info->format[PSC_VOLTAGE_OUT] = direct;
135 break;
136 default:
137 ret = -ENODEV;
138 goto abort;
144 * We should check if the COEFFICIENTS register is supported.
145 * If it is, and the chip is configured for direct mode, we can read
146 * the coefficients from the chip, one set per group of sensor
147 * registers.
149 * To do this, we will need access to a chip which actually supports the
150 * COEFFICIENTS command, since the command is too complex to implement
151 * without testing it. Until then, abort if a chip configured for direct
152 * mode was detected.
154 if (info->format[PSC_VOLTAGE_OUT] == direct) {
155 ret = -ENODEV;
156 goto abort;
159 /* Try to find sensor groups */
160 pmbus_find_sensor_groups(client, info);
161 abort:
162 return ret;
165 static int pmbus_probe(struct i2c_client *client,
166 const struct i2c_device_id *id)
168 struct pmbus_driver_info *info;
169 int ret;
171 info = kzalloc(sizeof(struct pmbus_driver_info), GFP_KERNEL);
172 if (!info)
173 return -ENOMEM;
175 info->pages = id->driver_data;
176 info->identify = pmbus_identify;
178 ret = pmbus_do_probe(client, id, info);
179 if (ret < 0)
180 goto out;
181 return 0;
183 out:
184 kfree(info);
185 return ret;
188 static int pmbus_remove(struct i2c_client *client)
190 const struct pmbus_driver_info *info;
192 info = pmbus_get_driver_info(client);
193 pmbus_do_remove(client);
194 kfree(info);
195 return 0;
199 * Use driver_data to set the number of pages supported by the chip.
201 static const struct i2c_device_id pmbus_id[] = {
202 {"adp4000", 1},
203 {"bmr450", 1},
204 {"bmr451", 1},
205 {"bmr453", 1},
206 {"bmr454", 1},
207 {"ncp4200", 1},
208 {"ncp4208", 1},
209 {"pdt003", 1},
210 {"pdt006", 1},
211 {"pdt012", 1},
212 {"pmbus", 0},
213 {"udt020", 1},
217 MODULE_DEVICE_TABLE(i2c, pmbus_id);
219 /* This is the driver that will be inserted */
220 static struct i2c_driver pmbus_driver = {
221 .driver = {
222 .name = "pmbus",
224 .probe = pmbus_probe,
225 .remove = pmbus_remove,
226 .id_table = pmbus_id,
229 static int __init pmbus_init(void)
231 return i2c_add_driver(&pmbus_driver);
234 static void __exit pmbus_exit(void)
236 i2c_del_driver(&pmbus_driver);
239 MODULE_AUTHOR("Guenter Roeck");
240 MODULE_DESCRIPTION("Generic PMBus driver");
241 MODULE_LICENSE("GPL");
242 module_init(pmbus_init);
243 module_exit(pmbus_exit);