blob: 9b1f0c37ef77bb22d0c14bcf43e891b7e7a6eb4f [file] [log] [blame]
Guenter Roeck442aba72011-01-26 20:09:02 -08001/*
2 * Hardware monitoring driver for PMBus devices
3 *
4 * Copyright (c) 2010, 2011 Ericsson AB.
5 *
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.
10 *
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.
15 *
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.
19 */
20
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"
29
30/*
31 * Find sensor groups and status registers on each page.
32 */
33static void pmbus_find_sensor_groups(struct i2c_client *client,
34 struct pmbus_driver_info *info)
35{
36 int page;
37
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;
Guenter Roeck81ae6812011-06-30 06:54:05 -070050 if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_12) &&
51 pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_1)) {
Guenter Roeck442aba72011-01-26 20:09:02 -080052 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;
55 }
Guenter Roeck81ae6812011-06-30 06:54:05 -070056 if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_34) &&
57 pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_3)) {
Guenter Roeck442aba72011-01-26 20:09:02 -080058 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;
61 }
Guenter Roeck22e6b232011-07-03 13:08:03 -070062 if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_1))
Guenter Roeck442aba72011-01-26 20:09:02 -080063 info->func[0] |= PMBUS_HAVE_TEMP;
Guenter Roeck0e502ec2011-06-30 06:57:41 -070064 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;
Guenter Roeck22e6b232011-07-03 13:08:03 -070068 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;
Guenter Roeck442aba72011-01-26 20:09:02 -080073
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;
81 }
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;
87 }
88 if (pmbus_check_word_register(client, page, PMBUS_READ_POUT))
89 info->func[page] |= PMBUS_HAVE_POUT;
90 }
91}
92
93/*
94 * Identify chip parameters.
95 */
96static int pmbus_identify(struct i2c_client *client,
97 struct pmbus_driver_info *info)
98{
99 if (!info->pages) {
100 /*
101 * Check if the PAGE command is supported. If it is,
102 * keep setting the page number until it fails or until the
103 * maximum number of pages has been reached. Assume that
104 * this is the number of pages supported by the chip.
105 */
106 if (pmbus_check_byte_register(client, 0, PMBUS_PAGE)) {
107 int page;
108
109 for (page = 1; page < PMBUS_PAGES; page++) {
110 if (pmbus_set_page(client, page) < 0)
111 break;
112 }
113 pmbus_set_page(client, 0);
114 info->pages = page;
115 } else {
116 info->pages = 1;
117 }
118 }
119
120 /*
121 * We should check if the COEFFICIENTS register is supported.
122 * If it is, and the chip is configured for direct mode, we can read
123 * the coefficients from the chip, one set per group of sensor
124 * registers.
125 *
126 * To do this, we will need access to a chip which actually supports the
127 * COEFFICIENTS command, since the command is too complex to implement
128 * without testing it.
129 */
130
131 /* Try to find sensor groups */
132 pmbus_find_sensor_groups(client, info);
133
134 return 0;
135}
136
137static int pmbus_probe(struct i2c_client *client,
138 const struct i2c_device_id *id)
139{
140 struct pmbus_driver_info *info;
141 int ret;
142
143 info = kzalloc(sizeof(struct pmbus_driver_info), GFP_KERNEL);
144 if (!info)
145 return -ENOMEM;
146
147 info->pages = id->driver_data;
148 info->identify = pmbus_identify;
149
150 ret = pmbus_do_probe(client, id, info);
151 if (ret < 0)
152 goto out;
153 return 0;
154
155out:
156 kfree(info);
157 return ret;
158}
159
160static int pmbus_remove(struct i2c_client *client)
161{
162 int ret;
163 const struct pmbus_driver_info *info;
164
165 info = pmbus_get_driver_info(client);
166 ret = pmbus_do_remove(client);
167 kfree(info);
168 return ret;
169}
170
171/*
172 * Use driver_data to set the number of pages supported by the chip.
173 */
174static const struct i2c_device_id pmbus_id[] = {
175 {"bmr450", 1},
176 {"bmr451", 1},
177 {"bmr453", 1},
178 {"bmr454", 1},
179 {"ltc2978", 8},
180 {"pmbus", 0},
181 {}
182};
183
184MODULE_DEVICE_TABLE(i2c, pmbus_id);
185
186/* This is the driver that will be inserted */
187static struct i2c_driver pmbus_driver = {
188 .driver = {
189 .name = "pmbus",
190 },
191 .probe = pmbus_probe,
192 .remove = pmbus_remove,
193 .id_table = pmbus_id,
194};
195
196static int __init pmbus_init(void)
197{
198 return i2c_add_driver(&pmbus_driver);
199}
200
201static void __exit pmbus_exit(void)
202{
203 i2c_del_driver(&pmbus_driver);
204}
205
206MODULE_AUTHOR("Guenter Roeck");
207MODULE_DESCRIPTION("Generic PMBus driver");
208MODULE_LICENSE("GPL");
209module_init(pmbus_init);
210module_exit(pmbus_exit);