xref: /linux/drivers/hwmon/pmbus/pmbus.c (revision 9d2ecfb768bd2f8b41816a23b0f1dda026fef41d)
1*9d2ecfb7SGuenter Roeck /*
2*9d2ecfb7SGuenter Roeck  * Hardware monitoring driver for PMBus devices
3*9d2ecfb7SGuenter Roeck  *
4*9d2ecfb7SGuenter Roeck  * Copyright (c) 2010, 2011 Ericsson AB.
5*9d2ecfb7SGuenter Roeck  *
6*9d2ecfb7SGuenter Roeck  * This program is free software; you can redistribute it and/or modify
7*9d2ecfb7SGuenter Roeck  * it under the terms of the GNU General Public License as published by
8*9d2ecfb7SGuenter Roeck  * the Free Software Foundation; either version 2 of the License, or
9*9d2ecfb7SGuenter Roeck  * (at your option) any later version.
10*9d2ecfb7SGuenter Roeck  *
11*9d2ecfb7SGuenter Roeck  * This program is distributed in the hope that it will be useful,
12*9d2ecfb7SGuenter Roeck  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13*9d2ecfb7SGuenter Roeck  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*9d2ecfb7SGuenter Roeck  * GNU General Public License for more details.
15*9d2ecfb7SGuenter Roeck  *
16*9d2ecfb7SGuenter Roeck  * You should have received a copy of the GNU General Public License
17*9d2ecfb7SGuenter Roeck  * along with this program; if not, write to the Free Software
18*9d2ecfb7SGuenter Roeck  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*9d2ecfb7SGuenter Roeck  */
20*9d2ecfb7SGuenter Roeck 
21*9d2ecfb7SGuenter Roeck #include <linux/kernel.h>
22*9d2ecfb7SGuenter Roeck #include <linux/module.h>
23*9d2ecfb7SGuenter Roeck #include <linux/init.h>
24*9d2ecfb7SGuenter Roeck #include <linux/err.h>
25*9d2ecfb7SGuenter Roeck #include <linux/slab.h>
26*9d2ecfb7SGuenter Roeck #include <linux/mutex.h>
27*9d2ecfb7SGuenter Roeck #include <linux/i2c.h>
28*9d2ecfb7SGuenter Roeck #include "pmbus.h"
29*9d2ecfb7SGuenter Roeck 
30*9d2ecfb7SGuenter Roeck /*
31*9d2ecfb7SGuenter Roeck  * Find sensor groups and status registers on each page.
32*9d2ecfb7SGuenter Roeck  */
33*9d2ecfb7SGuenter Roeck static void pmbus_find_sensor_groups(struct i2c_client *client,
34*9d2ecfb7SGuenter Roeck 				     struct pmbus_driver_info *info)
35*9d2ecfb7SGuenter Roeck {
36*9d2ecfb7SGuenter Roeck 	int page;
37*9d2ecfb7SGuenter Roeck 
38*9d2ecfb7SGuenter Roeck 	/* Sensors detected on page 0 only */
39*9d2ecfb7SGuenter Roeck 	if (pmbus_check_word_register(client, 0, PMBUS_READ_VIN))
40*9d2ecfb7SGuenter Roeck 		info->func[0] |= PMBUS_HAVE_VIN;
41*9d2ecfb7SGuenter Roeck 	if (pmbus_check_word_register(client, 0, PMBUS_READ_VCAP))
42*9d2ecfb7SGuenter Roeck 		info->func[0] |= PMBUS_HAVE_VCAP;
43*9d2ecfb7SGuenter Roeck 	if (pmbus_check_word_register(client, 0, PMBUS_READ_IIN))
44*9d2ecfb7SGuenter Roeck 		info->func[0] |= PMBUS_HAVE_IIN;
45*9d2ecfb7SGuenter Roeck 	if (pmbus_check_word_register(client, 0, PMBUS_READ_PIN))
46*9d2ecfb7SGuenter Roeck 		info->func[0] |= PMBUS_HAVE_PIN;
47*9d2ecfb7SGuenter Roeck 	if (info->func[0]
48*9d2ecfb7SGuenter Roeck 	    && pmbus_check_byte_register(client, 0, PMBUS_STATUS_INPUT))
49*9d2ecfb7SGuenter Roeck 		info->func[0] |= PMBUS_HAVE_STATUS_INPUT;
50*9d2ecfb7SGuenter Roeck 	if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_12) &&
51*9d2ecfb7SGuenter Roeck 	    pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_1)) {
52*9d2ecfb7SGuenter Roeck 		info->func[0] |= PMBUS_HAVE_FAN12;
53*9d2ecfb7SGuenter Roeck 		if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_12))
54*9d2ecfb7SGuenter Roeck 			info->func[0] |= PMBUS_HAVE_STATUS_FAN12;
55*9d2ecfb7SGuenter Roeck 	}
56*9d2ecfb7SGuenter Roeck 	if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_34) &&
57*9d2ecfb7SGuenter Roeck 	    pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_3)) {
58*9d2ecfb7SGuenter Roeck 		info->func[0] |= PMBUS_HAVE_FAN34;
59*9d2ecfb7SGuenter Roeck 		if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_34))
60*9d2ecfb7SGuenter Roeck 			info->func[0] |= PMBUS_HAVE_STATUS_FAN34;
61*9d2ecfb7SGuenter Roeck 	}
62*9d2ecfb7SGuenter Roeck 	if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_1))
63*9d2ecfb7SGuenter Roeck 		info->func[0] |= PMBUS_HAVE_TEMP;
64*9d2ecfb7SGuenter Roeck 	if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_2))
65*9d2ecfb7SGuenter Roeck 		info->func[0] |= PMBUS_HAVE_TEMP2;
66*9d2ecfb7SGuenter Roeck 	if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_3))
67*9d2ecfb7SGuenter Roeck 		info->func[0] |= PMBUS_HAVE_TEMP3;
68*9d2ecfb7SGuenter Roeck 	if (info->func[0] & (PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2
69*9d2ecfb7SGuenter Roeck 			     | PMBUS_HAVE_TEMP3)
70*9d2ecfb7SGuenter Roeck 	    && pmbus_check_byte_register(client, 0,
71*9d2ecfb7SGuenter Roeck 					 PMBUS_STATUS_TEMPERATURE))
72*9d2ecfb7SGuenter Roeck 			info->func[0] |= PMBUS_HAVE_STATUS_TEMP;
73*9d2ecfb7SGuenter Roeck 
74*9d2ecfb7SGuenter Roeck 	/* Sensors detected on all pages */
75*9d2ecfb7SGuenter Roeck 	for (page = 0; page < info->pages; page++) {
76*9d2ecfb7SGuenter Roeck 		if (pmbus_check_word_register(client, page, PMBUS_READ_VOUT)) {
77*9d2ecfb7SGuenter Roeck 			info->func[page] |= PMBUS_HAVE_VOUT;
78*9d2ecfb7SGuenter Roeck 			if (pmbus_check_byte_register(client, page,
79*9d2ecfb7SGuenter Roeck 						      PMBUS_STATUS_VOUT))
80*9d2ecfb7SGuenter Roeck 				info->func[page] |= PMBUS_HAVE_STATUS_VOUT;
81*9d2ecfb7SGuenter Roeck 		}
82*9d2ecfb7SGuenter Roeck 		if (pmbus_check_word_register(client, page, PMBUS_READ_IOUT)) {
83*9d2ecfb7SGuenter Roeck 			info->func[page] |= PMBUS_HAVE_IOUT;
84*9d2ecfb7SGuenter Roeck 			if (pmbus_check_byte_register(client, 0,
85*9d2ecfb7SGuenter Roeck 						      PMBUS_STATUS_IOUT))
86*9d2ecfb7SGuenter Roeck 				info->func[page] |= PMBUS_HAVE_STATUS_IOUT;
87*9d2ecfb7SGuenter Roeck 		}
88*9d2ecfb7SGuenter Roeck 		if (pmbus_check_word_register(client, page, PMBUS_READ_POUT))
89*9d2ecfb7SGuenter Roeck 			info->func[page] |= PMBUS_HAVE_POUT;
90*9d2ecfb7SGuenter Roeck 	}
91*9d2ecfb7SGuenter Roeck }
92*9d2ecfb7SGuenter Roeck 
93*9d2ecfb7SGuenter Roeck /*
94*9d2ecfb7SGuenter Roeck  * Identify chip parameters.
95*9d2ecfb7SGuenter Roeck  */
96*9d2ecfb7SGuenter Roeck static int pmbus_identify(struct i2c_client *client,
97*9d2ecfb7SGuenter Roeck 			  struct pmbus_driver_info *info)
98*9d2ecfb7SGuenter Roeck {
99*9d2ecfb7SGuenter Roeck 	if (!info->pages) {
100*9d2ecfb7SGuenter Roeck 		/*
101*9d2ecfb7SGuenter Roeck 		 * Check if the PAGE command is supported. If it is,
102*9d2ecfb7SGuenter Roeck 		 * keep setting the page number until it fails or until the
103*9d2ecfb7SGuenter Roeck 		 * maximum number of pages has been reached. Assume that
104*9d2ecfb7SGuenter Roeck 		 * this is the number of pages supported by the chip.
105*9d2ecfb7SGuenter Roeck 		 */
106*9d2ecfb7SGuenter Roeck 		if (pmbus_check_byte_register(client, 0, PMBUS_PAGE)) {
107*9d2ecfb7SGuenter Roeck 			int page;
108*9d2ecfb7SGuenter Roeck 
109*9d2ecfb7SGuenter Roeck 			for (page = 1; page < PMBUS_PAGES; page++) {
110*9d2ecfb7SGuenter Roeck 				if (pmbus_set_page(client, page) < 0)
111*9d2ecfb7SGuenter Roeck 					break;
112*9d2ecfb7SGuenter Roeck 			}
113*9d2ecfb7SGuenter Roeck 			pmbus_set_page(client, 0);
114*9d2ecfb7SGuenter Roeck 			info->pages = page;
115*9d2ecfb7SGuenter Roeck 		} else {
116*9d2ecfb7SGuenter Roeck 			info->pages = 1;
117*9d2ecfb7SGuenter Roeck 		}
118*9d2ecfb7SGuenter Roeck 	}
119*9d2ecfb7SGuenter Roeck 
120*9d2ecfb7SGuenter Roeck 	/*
121*9d2ecfb7SGuenter Roeck 	 * We should check if the COEFFICIENTS register is supported.
122*9d2ecfb7SGuenter Roeck 	 * If it is, and the chip is configured for direct mode, we can read
123*9d2ecfb7SGuenter Roeck 	 * the coefficients from the chip, one set per group of sensor
124*9d2ecfb7SGuenter Roeck 	 * registers.
125*9d2ecfb7SGuenter Roeck 	 *
126*9d2ecfb7SGuenter Roeck 	 * To do this, we will need access to a chip which actually supports the
127*9d2ecfb7SGuenter Roeck 	 * COEFFICIENTS command, since the command is too complex to implement
128*9d2ecfb7SGuenter Roeck 	 * without testing it.
129*9d2ecfb7SGuenter Roeck 	 */
130*9d2ecfb7SGuenter Roeck 
131*9d2ecfb7SGuenter Roeck 	/* Try to find sensor groups  */
132*9d2ecfb7SGuenter Roeck 	pmbus_find_sensor_groups(client, info);
133*9d2ecfb7SGuenter Roeck 
134*9d2ecfb7SGuenter Roeck 	return 0;
135*9d2ecfb7SGuenter Roeck }
136*9d2ecfb7SGuenter Roeck 
137*9d2ecfb7SGuenter Roeck static int pmbus_probe(struct i2c_client *client,
138*9d2ecfb7SGuenter Roeck 		       const struct i2c_device_id *id)
139*9d2ecfb7SGuenter Roeck {
140*9d2ecfb7SGuenter Roeck 	struct pmbus_driver_info *info;
141*9d2ecfb7SGuenter Roeck 	int ret;
142*9d2ecfb7SGuenter Roeck 
143*9d2ecfb7SGuenter Roeck 	info = kzalloc(sizeof(struct pmbus_driver_info), GFP_KERNEL);
144*9d2ecfb7SGuenter Roeck 	if (!info)
145*9d2ecfb7SGuenter Roeck 		return -ENOMEM;
146*9d2ecfb7SGuenter Roeck 
147*9d2ecfb7SGuenter Roeck 	info->pages = id->driver_data;
148*9d2ecfb7SGuenter Roeck 	info->identify = pmbus_identify;
149*9d2ecfb7SGuenter Roeck 
150*9d2ecfb7SGuenter Roeck 	ret = pmbus_do_probe(client, id, info);
151*9d2ecfb7SGuenter Roeck 	if (ret < 0)
152*9d2ecfb7SGuenter Roeck 		goto out;
153*9d2ecfb7SGuenter Roeck 	return 0;
154*9d2ecfb7SGuenter Roeck 
155*9d2ecfb7SGuenter Roeck out:
156*9d2ecfb7SGuenter Roeck 	kfree(info);
157*9d2ecfb7SGuenter Roeck 	return ret;
158*9d2ecfb7SGuenter Roeck }
159*9d2ecfb7SGuenter Roeck 
160*9d2ecfb7SGuenter Roeck static int pmbus_remove(struct i2c_client *client)
161*9d2ecfb7SGuenter Roeck {
162*9d2ecfb7SGuenter Roeck 	int ret;
163*9d2ecfb7SGuenter Roeck 	const struct pmbus_driver_info *info;
164*9d2ecfb7SGuenter Roeck 
165*9d2ecfb7SGuenter Roeck 	info = pmbus_get_driver_info(client);
166*9d2ecfb7SGuenter Roeck 	ret = pmbus_do_remove(client);
167*9d2ecfb7SGuenter Roeck 	kfree(info);
168*9d2ecfb7SGuenter Roeck 	return ret;
169*9d2ecfb7SGuenter Roeck }
170*9d2ecfb7SGuenter Roeck 
171*9d2ecfb7SGuenter Roeck /*
172*9d2ecfb7SGuenter Roeck  * Use driver_data to set the number of pages supported by the chip.
173*9d2ecfb7SGuenter Roeck  */
174*9d2ecfb7SGuenter Roeck static const struct i2c_device_id pmbus_id[] = {
175*9d2ecfb7SGuenter Roeck 	{"bmr450", 1},
176*9d2ecfb7SGuenter Roeck 	{"bmr451", 1},
177*9d2ecfb7SGuenter Roeck 	{"bmr453", 1},
178*9d2ecfb7SGuenter Roeck 	{"bmr454", 1},
179*9d2ecfb7SGuenter Roeck 	{"ltc2978", 8},
180*9d2ecfb7SGuenter Roeck 	{"pmbus", 0},
181*9d2ecfb7SGuenter Roeck 	{}
182*9d2ecfb7SGuenter Roeck };
183*9d2ecfb7SGuenter Roeck 
184*9d2ecfb7SGuenter Roeck MODULE_DEVICE_TABLE(i2c, pmbus_id);
185*9d2ecfb7SGuenter Roeck 
186*9d2ecfb7SGuenter Roeck /* This is the driver that will be inserted */
187*9d2ecfb7SGuenter Roeck static struct i2c_driver pmbus_driver = {
188*9d2ecfb7SGuenter Roeck 	.driver = {
189*9d2ecfb7SGuenter Roeck 		   .name = "pmbus",
190*9d2ecfb7SGuenter Roeck 		   },
191*9d2ecfb7SGuenter Roeck 	.probe = pmbus_probe,
192*9d2ecfb7SGuenter Roeck 	.remove = pmbus_remove,
193*9d2ecfb7SGuenter Roeck 	.id_table = pmbus_id,
194*9d2ecfb7SGuenter Roeck };
195*9d2ecfb7SGuenter Roeck 
196*9d2ecfb7SGuenter Roeck static int __init pmbus_init(void)
197*9d2ecfb7SGuenter Roeck {
198*9d2ecfb7SGuenter Roeck 	return i2c_add_driver(&pmbus_driver);
199*9d2ecfb7SGuenter Roeck }
200*9d2ecfb7SGuenter Roeck 
201*9d2ecfb7SGuenter Roeck static void __exit pmbus_exit(void)
202*9d2ecfb7SGuenter Roeck {
203*9d2ecfb7SGuenter Roeck 	i2c_del_driver(&pmbus_driver);
204*9d2ecfb7SGuenter Roeck }
205*9d2ecfb7SGuenter Roeck 
206*9d2ecfb7SGuenter Roeck MODULE_AUTHOR("Guenter Roeck");
207*9d2ecfb7SGuenter Roeck MODULE_DESCRIPTION("Generic PMBus driver");
208*9d2ecfb7SGuenter Roeck MODULE_LICENSE("GPL");
209*9d2ecfb7SGuenter Roeck module_init(pmbus_init);
210*9d2ecfb7SGuenter Roeck module_exit(pmbus_exit);
211