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/pmbus.h>
31 struct pmbus_device_info
{
37 * Find sensor groups and status registers on each page.
39 static void pmbus_find_sensor_groups(struct i2c_client
*client
,
40 struct pmbus_driver_info
*info
)
44 /* Sensors detected on page 0 only */
45 if (pmbus_check_word_register(client
, 0, PMBUS_READ_VIN
))
46 info
->func
[0] |= PMBUS_HAVE_VIN
;
47 if (pmbus_check_word_register(client
, 0, PMBUS_READ_VCAP
))
48 info
->func
[0] |= PMBUS_HAVE_VCAP
;
49 if (pmbus_check_word_register(client
, 0, PMBUS_READ_IIN
))
50 info
->func
[0] |= PMBUS_HAVE_IIN
;
51 if (pmbus_check_word_register(client
, 0, PMBUS_READ_PIN
))
52 info
->func
[0] |= PMBUS_HAVE_PIN
;
54 && pmbus_check_byte_register(client
, 0, PMBUS_STATUS_INPUT
))
55 info
->func
[0] |= PMBUS_HAVE_STATUS_INPUT
;
56 if (pmbus_check_byte_register(client
, 0, PMBUS_FAN_CONFIG_12
) &&
57 pmbus_check_word_register(client
, 0, PMBUS_READ_FAN_SPEED_1
)) {
58 info
->func
[0] |= PMBUS_HAVE_FAN12
;
59 if (pmbus_check_byte_register(client
, 0, PMBUS_STATUS_FAN_12
))
60 info
->func
[0] |= PMBUS_HAVE_STATUS_FAN12
;
62 if (pmbus_check_byte_register(client
, 0, PMBUS_FAN_CONFIG_34
) &&
63 pmbus_check_word_register(client
, 0, PMBUS_READ_FAN_SPEED_3
)) {
64 info
->func
[0] |= PMBUS_HAVE_FAN34
;
65 if (pmbus_check_byte_register(client
, 0, PMBUS_STATUS_FAN_34
))
66 info
->func
[0] |= PMBUS_HAVE_STATUS_FAN34
;
68 if (pmbus_check_word_register(client
, 0, PMBUS_READ_TEMPERATURE_1
))
69 info
->func
[0] |= PMBUS_HAVE_TEMP
;
70 if (pmbus_check_word_register(client
, 0, PMBUS_READ_TEMPERATURE_2
))
71 info
->func
[0] |= PMBUS_HAVE_TEMP2
;
72 if (pmbus_check_word_register(client
, 0, PMBUS_READ_TEMPERATURE_3
))
73 info
->func
[0] |= PMBUS_HAVE_TEMP3
;
74 if (info
->func
[0] & (PMBUS_HAVE_TEMP
| PMBUS_HAVE_TEMP2
76 && pmbus_check_byte_register(client
, 0,
77 PMBUS_STATUS_TEMPERATURE
))
78 info
->func
[0] |= PMBUS_HAVE_STATUS_TEMP
;
80 /* Sensors detected on all pages */
81 for (page
= 0; page
< info
->pages
; page
++) {
82 if (pmbus_check_word_register(client
, page
, PMBUS_READ_VOUT
)) {
83 info
->func
[page
] |= PMBUS_HAVE_VOUT
;
84 if (pmbus_check_byte_register(client
, page
,
86 info
->func
[page
] |= PMBUS_HAVE_STATUS_VOUT
;
88 if (pmbus_check_word_register(client
, page
, PMBUS_READ_IOUT
)) {
89 info
->func
[page
] |= PMBUS_HAVE_IOUT
;
90 if (pmbus_check_byte_register(client
, 0,
92 info
->func
[page
] |= PMBUS_HAVE_STATUS_IOUT
;
94 if (pmbus_check_word_register(client
, page
, PMBUS_READ_POUT
))
95 info
->func
[page
] |= PMBUS_HAVE_POUT
;
100 * Identify chip parameters.
102 static int pmbus_identify(struct i2c_client
*client
,
103 struct pmbus_driver_info
*info
)
109 * Check if the PAGE command is supported. If it is,
110 * keep setting the page number until it fails or until the
111 * maximum number of pages has been reached. Assume that
112 * this is the number of pages supported by the chip.
114 if (pmbus_check_byte_register(client
, 0, PMBUS_PAGE
)) {
117 for (page
= 1; page
< PMBUS_PAGES
; page
++) {
118 if (pmbus_set_page(client
, page
) < 0)
121 pmbus_set_page(client
, 0);
127 pmbus_clear_faults(client
);
130 if (pmbus_check_byte_register(client
, 0, PMBUS_VOUT_MODE
)) {
133 vout_mode
= pmbus_read_byte_data(client
, 0, PMBUS_VOUT_MODE
);
134 if (vout_mode
>= 0 && vout_mode
!= 0xff) {
135 switch (vout_mode
>> 5) {
139 info
->format
[PSC_VOLTAGE_OUT
] = vid
;
140 info
->vrm_version
= vr11
;
143 info
->format
[PSC_VOLTAGE_OUT
] = direct
;
153 * We should check if the COEFFICIENTS register is supported.
154 * If it is, and the chip is configured for direct mode, we can read
155 * the coefficients from the chip, one set per group of sensor
158 * To do this, we will need access to a chip which actually supports the
159 * COEFFICIENTS command, since the command is too complex to implement
160 * without testing it. Until then, abort if a chip configured for direct
163 if (info
->format
[PSC_VOLTAGE_OUT
] == direct
) {
168 /* Try to find sensor groups */
169 pmbus_find_sensor_groups(client
, info
);
174 static int pmbus_probe(struct i2c_client
*client
,
175 const struct i2c_device_id
*id
)
177 struct pmbus_driver_info
*info
;
178 struct pmbus_platform_data
*pdata
= NULL
;
179 struct device
*dev
= &client
->dev
;
180 struct pmbus_device_info
*device_info
;
182 info
= devm_kzalloc(dev
, sizeof(struct pmbus_driver_info
), GFP_KERNEL
);
186 device_info
= (struct pmbus_device_info
*)id
->driver_data
;
187 if (device_info
->flags
& PMBUS_SKIP_STATUS_CHECK
) {
188 pdata
= devm_kzalloc(dev
, sizeof(struct pmbus_platform_data
),
193 pdata
->flags
= PMBUS_SKIP_STATUS_CHECK
;
196 info
->pages
= device_info
->pages
;
197 info
->identify
= pmbus_identify
;
198 dev
->platform_data
= pdata
;
200 return pmbus_do_probe(client
, id
, info
);
203 static const struct pmbus_device_info pmbus_info_one
= {
207 static const struct pmbus_device_info pmbus_info_zero
= {
211 static const struct pmbus_device_info pmbus_info_one_skip
= {
213 .flags
= PMBUS_SKIP_STATUS_CHECK
217 * Use driver_data to set the number of pages supported by the chip.
219 static const struct i2c_device_id pmbus_id
[] = {
220 {"adp4000", (kernel_ulong_t
)&pmbus_info_one
},
221 {"bmr453", (kernel_ulong_t
)&pmbus_info_one
},
222 {"bmr454", (kernel_ulong_t
)&pmbus_info_one
},
223 {"dps460", (kernel_ulong_t
)&pmbus_info_one_skip
},
224 {"dps650ab", (kernel_ulong_t
)&pmbus_info_one_skip
},
225 {"dps800", (kernel_ulong_t
)&pmbus_info_one_skip
},
226 {"mdt040", (kernel_ulong_t
)&pmbus_info_one
},
227 {"ncp4200", (kernel_ulong_t
)&pmbus_info_one
},
228 {"ncp4208", (kernel_ulong_t
)&pmbus_info_one
},
229 {"pdt003", (kernel_ulong_t
)&pmbus_info_one
},
230 {"pdt006", (kernel_ulong_t
)&pmbus_info_one
},
231 {"pdt012", (kernel_ulong_t
)&pmbus_info_one
},
232 {"pmbus", (kernel_ulong_t
)&pmbus_info_zero
},
233 {"sgd009", (kernel_ulong_t
)&pmbus_info_one_skip
},
234 {"tps40400", (kernel_ulong_t
)&pmbus_info_one
},
235 {"tps544b20", (kernel_ulong_t
)&pmbus_info_one
},
236 {"tps544b25", (kernel_ulong_t
)&pmbus_info_one
},
237 {"tps544c20", (kernel_ulong_t
)&pmbus_info_one
},
238 {"tps544c25", (kernel_ulong_t
)&pmbus_info_one
},
239 {"udt020", (kernel_ulong_t
)&pmbus_info_one
},
243 MODULE_DEVICE_TABLE(i2c
, pmbus_id
);
245 /* This is the driver that will be inserted */
246 static struct i2c_driver pmbus_driver
= {
250 .probe
= pmbus_probe
,
251 .remove
= pmbus_do_remove
,
252 .id_table
= pmbus_id
,
255 module_i2c_driver(pmbus_driver
);
257 MODULE_AUTHOR("Guenter Roeck");
258 MODULE_DESCRIPTION("Generic PMBus driver");
259 MODULE_LICENSE("GPL");