GNU Linux-libre 4.9.328-gnu1
[releases.git] / drivers / hwmon / pmbus / pmbus.c
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 <linux/i2c/pmbus.h>
29 #include "pmbus.h"
30
31 /*
32  * Find sensor groups and status registers on each page.
33  */
34 static void pmbus_find_sensor_groups(struct i2c_client *client,
35                                      struct pmbus_driver_info *info)
36 {
37         int page;
38
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;
48         if (info->func[0]
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;
56         }
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;
62         }
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
70                              | PMBUS_HAVE_TEMP3)
71             && pmbus_check_byte_register(client, 0,
72                                          PMBUS_STATUS_TEMPERATURE))
73                         info->func[0] |= PMBUS_HAVE_STATUS_TEMP;
74
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,
80                                                       PMBUS_STATUS_VOUT))
81                                 info->func[page] |= PMBUS_HAVE_STATUS_VOUT;
82                 }
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,
86                                                       PMBUS_STATUS_IOUT))
87                                 info->func[page] |= PMBUS_HAVE_STATUS_IOUT;
88                 }
89                 if (pmbus_check_word_register(client, page, PMBUS_READ_POUT))
90                         info->func[page] |= PMBUS_HAVE_POUT;
91         }
92 }
93
94 /*
95  * Identify chip parameters.
96  */
97 static int pmbus_identify(struct i2c_client *client,
98                           struct pmbus_driver_info *info)
99 {
100         int ret = 0;
101
102         if (!info->pages) {
103                 /*
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.
108                  */
109                 if (pmbus_check_byte_register(client, 0, PMBUS_PAGE)) {
110                         int page;
111
112                         for (page = 1; page < PMBUS_PAGES; page++) {
113                                 if (pmbus_set_page(client, page) < 0)
114                                         break;
115                         }
116                         pmbus_set_page(client, 0);
117                         info->pages = page;
118                 } else {
119                         info->pages = 1;
120                 }
121
122                 pmbus_clear_faults(client);
123         }
124
125         if (pmbus_check_byte_register(client, 0, PMBUS_VOUT_MODE)) {
126                 int vout_mode;
127
128                 vout_mode = pmbus_read_byte_data(client, 0, PMBUS_VOUT_MODE);
129                 if (vout_mode >= 0 && vout_mode != 0xff) {
130                         switch (vout_mode >> 5) {
131                         case 0:
132                                 break;
133                         case 1:
134                                 info->format[PSC_VOLTAGE_OUT] = vid;
135                                 info->vrm_version = vr11;
136                                 break;
137                         case 2:
138                                 info->format[PSC_VOLTAGE_OUT] = direct;
139                                 break;
140                         default:
141                                 ret = -ENODEV;
142                                 goto abort;
143                         }
144                 }
145         }
146
147         /*
148          * We should check if the COEFFICIENTS register is supported.
149          * If it is, and the chip is configured for direct mode, we can read
150          * the coefficients from the chip, one set per group of sensor
151          * registers.
152          *
153          * To do this, we will need access to a chip which actually supports the
154          * COEFFICIENTS command, since the command is too complex to implement
155          * without testing it. Until then, abort if a chip configured for direct
156          * mode was detected.
157          */
158         if (info->format[PSC_VOLTAGE_OUT] == direct) {
159                 ret = -ENODEV;
160                 goto abort;
161         }
162
163         /* Try to find sensor groups  */
164         pmbus_find_sensor_groups(client, info);
165 abort:
166         return ret;
167 }
168
169 static int pmbus_probe(struct i2c_client *client,
170                        const struct i2c_device_id *id)
171 {
172         struct pmbus_driver_info *info;
173         struct pmbus_platform_data *pdata = NULL;
174         struct device *dev = &client->dev;
175
176         info = devm_kzalloc(dev, sizeof(struct pmbus_driver_info), GFP_KERNEL);
177         if (!info)
178                 return -ENOMEM;
179
180         if (!strcmp(id->name, "dps460") || !strcmp(id->name, "dps800") ||
181             !strcmp(id->name, "sgd009")) {
182                 pdata = devm_kzalloc(dev, sizeof(struct pmbus_platform_data),
183                                      GFP_KERNEL);
184                 if (!pdata)
185                         return -ENOMEM;
186
187                 pdata->flags = PMBUS_SKIP_STATUS_CHECK;
188         }
189
190         info->pages = id->driver_data;
191         info->identify = pmbus_identify;
192         dev->platform_data = pdata;
193
194         return pmbus_do_probe(client, id, info);
195 }
196
197 /*
198  * Use driver_data to set the number of pages supported by the chip.
199  */
200 static const struct i2c_device_id pmbus_id[] = {
201         {"adp4000", 1},
202         {"bmr453", 1},
203         {"bmr454", 1},
204         {"dps460", 1},
205         {"dps800", 1},
206         {"mdt040", 1},
207         {"ncp4200", 1},
208         {"ncp4208", 1},
209         {"pdt003", 1},
210         {"pdt006", 1},
211         {"pdt012", 1},
212         {"pmbus", 0},
213         {"sgd009", 1},
214         {"tps40400", 1},
215         {"tps544b20", 1},
216         {"tps544b25", 1},
217         {"tps544c20", 1},
218         {"tps544c25", 1},
219         {"udt020", 1},
220         {}
221 };
222
223 MODULE_DEVICE_TABLE(i2c, pmbus_id);
224
225 /* This is the driver that will be inserted */
226 static struct i2c_driver pmbus_driver = {
227         .driver = {
228                    .name = "pmbus",
229                    },
230         .probe = pmbus_probe,
231         .remove = pmbus_do_remove,
232         .id_table = pmbus_id,
233 };
234
235 module_i2c_driver(pmbus_driver);
236
237 MODULE_AUTHOR("Guenter Roeck");
238 MODULE_DESCRIPTION("Generic PMBus driver");
239 MODULE_LICENSE("GPL");