hwmon: (pmbus) Improve fan detection
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / hwmon / pmbus.c
blobb0ea00b75f173556be047b9cba69359e1b512e00
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_byte_register(client, 0,
65 PMBUS_STATUS_TEMPERATURE))
66 info->func[0] |= PMBUS_HAVE_STATUS_TEMP;
69 /* Sensors detected on all pages */
70 for (page = 0; page < info->pages; page++) {
71 if (pmbus_check_word_register(client, page, PMBUS_READ_VOUT)) {
72 info->func[page] |= PMBUS_HAVE_VOUT;
73 if (pmbus_check_byte_register(client, page,
74 PMBUS_STATUS_VOUT))
75 info->func[page] |= PMBUS_HAVE_STATUS_VOUT;
77 if (pmbus_check_word_register(client, page, PMBUS_READ_IOUT)) {
78 info->func[page] |= PMBUS_HAVE_IOUT;
79 if (pmbus_check_byte_register(client, 0,
80 PMBUS_STATUS_IOUT))
81 info->func[page] |= PMBUS_HAVE_STATUS_IOUT;
83 if (pmbus_check_word_register(client, page, PMBUS_READ_POUT))
84 info->func[page] |= PMBUS_HAVE_POUT;
89 * Identify chip parameters.
91 static int pmbus_identify(struct i2c_client *client,
92 struct pmbus_driver_info *info)
94 if (!info->pages) {
96 * Check if the PAGE command is supported. If it is,
97 * keep setting the page number until it fails or until the
98 * maximum number of pages has been reached. Assume that
99 * this is the number of pages supported by the chip.
101 if (pmbus_check_byte_register(client, 0, PMBUS_PAGE)) {
102 int page;
104 for (page = 1; page < PMBUS_PAGES; page++) {
105 if (pmbus_set_page(client, page) < 0)
106 break;
108 pmbus_set_page(client, 0);
109 info->pages = page;
110 } else {
111 info->pages = 1;
116 * We should check if the COEFFICIENTS register is supported.
117 * If it is, and the chip is configured for direct mode, we can read
118 * the coefficients from the chip, one set per group of sensor
119 * registers.
121 * To do this, we will need access to a chip which actually supports the
122 * COEFFICIENTS command, since the command is too complex to implement
123 * without testing it.
126 /* Try to find sensor groups */
127 pmbus_find_sensor_groups(client, info);
129 return 0;
132 static int pmbus_probe(struct i2c_client *client,
133 const struct i2c_device_id *id)
135 struct pmbus_driver_info *info;
136 int ret;
138 info = kzalloc(sizeof(struct pmbus_driver_info), GFP_KERNEL);
139 if (!info)
140 return -ENOMEM;
142 info->pages = id->driver_data;
143 info->identify = pmbus_identify;
145 ret = pmbus_do_probe(client, id, info);
146 if (ret < 0)
147 goto out;
148 return 0;
150 out:
151 kfree(info);
152 return ret;
155 static int pmbus_remove(struct i2c_client *client)
157 int ret;
158 const struct pmbus_driver_info *info;
160 info = pmbus_get_driver_info(client);
161 ret = pmbus_do_remove(client);
162 kfree(info);
163 return ret;
167 * Use driver_data to set the number of pages supported by the chip.
169 static const struct i2c_device_id pmbus_id[] = {
170 {"bmr450", 1},
171 {"bmr451", 1},
172 {"bmr453", 1},
173 {"bmr454", 1},
174 {"ltc2978", 8},
175 {"pmbus", 0},
179 MODULE_DEVICE_TABLE(i2c, pmbus_id);
181 /* This is the driver that will be inserted */
182 static struct i2c_driver pmbus_driver = {
183 .driver = {
184 .name = "pmbus",
186 .probe = pmbus_probe,
187 .remove = pmbus_remove,
188 .id_table = pmbus_id,
191 static int __init pmbus_init(void)
193 return i2c_add_driver(&pmbus_driver);
196 static void __exit pmbus_exit(void)
198 i2c_del_driver(&pmbus_driver);
201 MODULE_AUTHOR("Guenter Roeck");
202 MODULE_DESCRIPTION("Generic PMBus driver");
203 MODULE_LICENSE("GPL");
204 module_init(pmbus_init);
205 module_exit(pmbus_exit);