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 <linux/i2c/pmbus.h>
32 * Find sensor groups and status registers on each page.
34 static void pmbus_find_sensor_groups(struct i2c_client
*client
,
35 struct pmbus_driver_info
*info
)
39 /* Sensors detected on page 0 only */
40 if (pmbus_check_word_register(client
, 0, PMBUS_READ_VIN
))
41 info
->func
[0] |= PMBUS_HAVE_VIN
;
42 if (pmbus_check_word_register(client
, 0, PMBUS_READ_VCAP
))
43 info
->func
[0] |= PMBUS_HAVE_VCAP
;
44 if (pmbus_check_word_register(client
, 0, PMBUS_READ_IIN
))
45 info
->func
[0] |= PMBUS_HAVE_IIN
;
46 if (pmbus_check_word_register(client
, 0, PMBUS_READ_PIN
))
47 info
->func
[0] |= PMBUS_HAVE_PIN
;
49 && pmbus_check_byte_register(client
, 0, PMBUS_STATUS_INPUT
))
50 info
->func
[0] |= PMBUS_HAVE_STATUS_INPUT
;
51 if (pmbus_check_byte_register(client
, 0, PMBUS_FAN_CONFIG_12
) &&
52 pmbus_check_word_register(client
, 0, PMBUS_READ_FAN_SPEED_1
)) {
53 info
->func
[0] |= PMBUS_HAVE_FAN12
;
54 if (pmbus_check_byte_register(client
, 0, PMBUS_STATUS_FAN_12
))
55 info
->func
[0] |= PMBUS_HAVE_STATUS_FAN12
;
57 if (pmbus_check_byte_register(client
, 0, PMBUS_FAN_CONFIG_34
) &&
58 pmbus_check_word_register(client
, 0, PMBUS_READ_FAN_SPEED_3
)) {
59 info
->func
[0] |= PMBUS_HAVE_FAN34
;
60 if (pmbus_check_byte_register(client
, 0, PMBUS_STATUS_FAN_34
))
61 info
->func
[0] |= PMBUS_HAVE_STATUS_FAN34
;
63 if (pmbus_check_word_register(client
, 0, PMBUS_READ_TEMPERATURE_1
))
64 info
->func
[0] |= PMBUS_HAVE_TEMP
;
65 if (pmbus_check_word_register(client
, 0, PMBUS_READ_TEMPERATURE_2
))
66 info
->func
[0] |= PMBUS_HAVE_TEMP2
;
67 if (pmbus_check_word_register(client
, 0, PMBUS_READ_TEMPERATURE_3
))
68 info
->func
[0] |= PMBUS_HAVE_TEMP3
;
69 if (info
->func
[0] & (PMBUS_HAVE_TEMP
| PMBUS_HAVE_TEMP2
71 && pmbus_check_byte_register(client
, 0,
72 PMBUS_STATUS_TEMPERATURE
))
73 info
->func
[0] |= PMBUS_HAVE_STATUS_TEMP
;
75 /* Sensors detected on all pages */
76 for (page
= 0; page
< info
->pages
; page
++) {
77 if (pmbus_check_word_register(client
, page
, PMBUS_READ_VOUT
)) {
78 info
->func
[page
] |= PMBUS_HAVE_VOUT
;
79 if (pmbus_check_byte_register(client
, page
,
81 info
->func
[page
] |= PMBUS_HAVE_STATUS_VOUT
;
83 if (pmbus_check_word_register(client
, page
, PMBUS_READ_IOUT
)) {
84 info
->func
[page
] |= PMBUS_HAVE_IOUT
;
85 if (pmbus_check_byte_register(client
, 0,
87 info
->func
[page
] |= PMBUS_HAVE_STATUS_IOUT
;
89 if (pmbus_check_word_register(client
, page
, PMBUS_READ_POUT
))
90 info
->func
[page
] |= PMBUS_HAVE_POUT
;
95 * Identify chip parameters.
97 static int pmbus_identify(struct i2c_client
*client
,
98 struct pmbus_driver_info
*info
)
104 * Check if the PAGE command is supported. If it is,
105 * keep setting the page number until it fails or until the
106 * maximum number of pages has been reached. Assume that
107 * this is the number of pages supported by the chip.
109 if (pmbus_check_byte_register(client
, 0, PMBUS_PAGE
)) {
112 for (page
= 1; page
< PMBUS_PAGES
; page
++) {
113 if (pmbus_set_page(client
, page
) < 0)
116 pmbus_set_page(client
, 0);
123 if (pmbus_check_byte_register(client
, 0, PMBUS_VOUT_MODE
)) {
126 vout_mode
= pmbus_read_byte_data(client
, 0, PMBUS_VOUT_MODE
);
127 if (vout_mode
>= 0 && vout_mode
!= 0xff) {
128 switch (vout_mode
>> 5) {
132 info
->format
[PSC_VOLTAGE_OUT
] = vid
;
133 info
->vrm_version
= vr11
;
136 info
->format
[PSC_VOLTAGE_OUT
] = direct
;
146 * We should check if the COEFFICIENTS register is supported.
147 * If it is, and the chip is configured for direct mode, we can read
148 * the coefficients from the chip, one set per group of sensor
151 * To do this, we will need access to a chip which actually supports the
152 * COEFFICIENTS command, since the command is too complex to implement
153 * without testing it. Until then, abort if a chip configured for direct
156 if (info
->format
[PSC_VOLTAGE_OUT
] == direct
) {
161 /* Try to find sensor groups */
162 pmbus_find_sensor_groups(client
, info
);
167 static int pmbus_probe(struct i2c_client
*client
,
168 const struct i2c_device_id
*id
)
170 struct pmbus_driver_info
*info
;
171 struct pmbus_platform_data
*pdata
= NULL
;
172 struct device
*dev
= &client
->dev
;
174 info
= devm_kzalloc(dev
, sizeof(struct pmbus_driver_info
), GFP_KERNEL
);
178 if (!strcmp(id
->name
, "dps460") || !strcmp(id
->name
, "dps800") ||
179 !strcmp(id
->name
, "sgd009")) {
180 pdata
= devm_kzalloc(dev
, sizeof(struct pmbus_platform_data
),
185 pdata
->flags
= PMBUS_SKIP_STATUS_CHECK
;
188 info
->pages
= id
->driver_data
;
189 info
->identify
= pmbus_identify
;
190 dev
->platform_data
= pdata
;
192 return pmbus_do_probe(client
, id
, info
);
196 * Use driver_data to set the number of pages supported by the chip.
198 static const struct i2c_device_id pmbus_id
[] = {
221 MODULE_DEVICE_TABLE(i2c
, pmbus_id
);
223 /* This is the driver that will be inserted */
224 static struct i2c_driver pmbus_driver
= {
228 .probe
= pmbus_probe
,
229 .remove
= pmbus_do_remove
,
230 .id_table
= pmbus_id
,
233 module_i2c_driver(pmbus_driver
);
235 MODULE_AUTHOR("Guenter Roeck");
236 MODULE_DESCRIPTION("Generic PMBus driver");
237 MODULE_LICENSE("GPL");