]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/hwmon/pmbus.c
hwmon: (pmbus) Auto-detect temp2 and temp3 registers/attributes
[mirror_ubuntu-artful-kernel.git] / drivers / hwmon / pmbus.c
CommitLineData
442aba78
GR
1/*
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;
81ae6814
GR
50 if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_12) &&
51 pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_1)) {
442aba78
GR
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;
55 }
81ae6814
GR
56 if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_34) &&
57 pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_3)) {
442aba78
GR
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;
61 }
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;
67 }
0e502ec8
GR
68 if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_2))
69 info->func[0] |= PMBUS_HAVE_TEMP2;
70 if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_3))
71 info->func[0] |= PMBUS_HAVE_TEMP3;
442aba78
GR
72
73 /* Sensors detected on all pages */
74 for (page = 0; page < info->pages; page++) {
75 if (pmbus_check_word_register(client, page, PMBUS_READ_VOUT)) {
76 info->func[page] |= PMBUS_HAVE_VOUT;
77 if (pmbus_check_byte_register(client, page,
78 PMBUS_STATUS_VOUT))
79 info->func[page] |= PMBUS_HAVE_STATUS_VOUT;
80 }
81 if (pmbus_check_word_register(client, page, PMBUS_READ_IOUT)) {
82 info->func[page] |= PMBUS_HAVE_IOUT;
83 if (pmbus_check_byte_register(client, 0,
84 PMBUS_STATUS_IOUT))
85 info->func[page] |= PMBUS_HAVE_STATUS_IOUT;
86 }
87 if (pmbus_check_word_register(client, page, PMBUS_READ_POUT))
88 info->func[page] |= PMBUS_HAVE_POUT;
89 }
90}
91
92/*
93 * Identify chip parameters.
94 */
95static int pmbus_identify(struct i2c_client *client,
96 struct pmbus_driver_info *info)
97{
98 if (!info->pages) {
99 /*
100 * Check if the PAGE command is supported. If it is,
101 * keep setting the page number until it fails or until the
102 * maximum number of pages has been reached. Assume that
103 * this is the number of pages supported by the chip.
104 */
105 if (pmbus_check_byte_register(client, 0, PMBUS_PAGE)) {
106 int page;
107
108 for (page = 1; page < PMBUS_PAGES; page++) {
109 if (pmbus_set_page(client, page) < 0)
110 break;
111 }
112 pmbus_set_page(client, 0);
113 info->pages = page;
114 } else {
115 info->pages = 1;
116 }
117 }
118
119 /*
120 * We should check if the COEFFICIENTS register is supported.
121 * If it is, and the chip is configured for direct mode, we can read
122 * the coefficients from the chip, one set per group of sensor
123 * registers.
124 *
125 * To do this, we will need access to a chip which actually supports the
126 * COEFFICIENTS command, since the command is too complex to implement
127 * without testing it.
128 */
129
130 /* Try to find sensor groups */
131 pmbus_find_sensor_groups(client, info);
132
133 return 0;
134}
135
136static int pmbus_probe(struct i2c_client *client,
137 const struct i2c_device_id *id)
138{
139 struct pmbus_driver_info *info;
140 int ret;
141
142 info = kzalloc(sizeof(struct pmbus_driver_info), GFP_KERNEL);
143 if (!info)
144 return -ENOMEM;
145
146 info->pages = id->driver_data;
147 info->identify = pmbus_identify;
148
149 ret = pmbus_do_probe(client, id, info);
150 if (ret < 0)
151 goto out;
152 return 0;
153
154out:
155 kfree(info);
156 return ret;
157}
158
159static int pmbus_remove(struct i2c_client *client)
160{
161 int ret;
162 const struct pmbus_driver_info *info;
163
164 info = pmbus_get_driver_info(client);
165 ret = pmbus_do_remove(client);
166 kfree(info);
167 return ret;
168}
169
170/*
171 * Use driver_data to set the number of pages supported by the chip.
172 */
173static const struct i2c_device_id pmbus_id[] = {
174 {"bmr450", 1},
175 {"bmr451", 1},
176 {"bmr453", 1},
177 {"bmr454", 1},
178 {"ltc2978", 8},
179 {"pmbus", 0},
180 {}
181};
182
183MODULE_DEVICE_TABLE(i2c, pmbus_id);
184
185/* This is the driver that will be inserted */
186static struct i2c_driver pmbus_driver = {
187 .driver = {
188 .name = "pmbus",
189 },
190 .probe = pmbus_probe,
191 .remove = pmbus_remove,
192 .id_table = pmbus_id,
193};
194
195static int __init pmbus_init(void)
196{
197 return i2c_add_driver(&pmbus_driver);
198}
199
200static void __exit pmbus_exit(void)
201{
202 i2c_del_driver(&pmbus_driver);
203}
204
205MODULE_AUTHOR("Guenter Roeck");
206MODULE_DESCRIPTION("Generic PMBus driver");
207MODULE_LICENSE("GPL");
208module_init(pmbus_init);
209module_exit(pmbus_exit);