]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/hwmon/pmbus/pmbus.c
Merge branch 'for-linus-4.13-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git...
[karo-tx-linux.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/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
123         if (pmbus_check_byte_register(client, 0, PMBUS_VOUT_MODE)) {
124                 int vout_mode;
125
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) {
129                         case 0:
130                                 break;
131                         case 1:
132                                 info->format[PSC_VOLTAGE_OUT] = vid;
133                                 info->vrm_version = vr11;
134                                 break;
135                         case 2:
136                                 info->format[PSC_VOLTAGE_OUT] = direct;
137                                 break;
138                         default:
139                                 ret = -ENODEV;
140                                 goto abort;
141                         }
142                 }
143         }
144
145         /*
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
149          * registers.
150          *
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
154          * mode was detected.
155          */
156         if (info->format[PSC_VOLTAGE_OUT] == direct) {
157                 ret = -ENODEV;
158                 goto abort;
159         }
160
161         /* Try to find sensor groups  */
162         pmbus_find_sensor_groups(client, info);
163 abort:
164         return ret;
165 }
166
167 static int pmbus_probe(struct i2c_client *client,
168                        const struct i2c_device_id *id)
169 {
170         struct pmbus_driver_info *info;
171         struct pmbus_platform_data *pdata = NULL;
172         struct device *dev = &client->dev;
173
174         info = devm_kzalloc(dev, sizeof(struct pmbus_driver_info), GFP_KERNEL);
175         if (!info)
176                 return -ENOMEM;
177
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),
181                                      GFP_KERNEL);
182                 if (!pdata)
183                         return -ENOMEM;
184
185                 pdata->flags = PMBUS_SKIP_STATUS_CHECK;
186         }
187
188         info->pages = id->driver_data;
189         info->identify = pmbus_identify;
190         dev->platform_data = pdata;
191
192         return pmbus_do_probe(client, id, info);
193 }
194
195 /*
196  * Use driver_data to set the number of pages supported by the chip.
197  */
198 static const struct i2c_device_id pmbus_id[] = {
199         {"adp4000", 1},
200         {"bmr453", 1},
201         {"bmr454", 1},
202         {"dps460", 1},
203         {"dps800", 1},
204         {"mdt040", 1},
205         {"ncp4200", 1},
206         {"ncp4208", 1},
207         {"pdt003", 1},
208         {"pdt006", 1},
209         {"pdt012", 1},
210         {"pmbus", 0},
211         {"sgd009", 1},
212         {"tps40400", 1},
213         {"tps544b20", 1},
214         {"tps544b25", 1},
215         {"tps544c20", 1},
216         {"tps544c25", 1},
217         {"udt020", 1},
218         {}
219 };
220
221 MODULE_DEVICE_TABLE(i2c, pmbus_id);
222
223 /* This is the driver that will be inserted */
224 static struct i2c_driver pmbus_driver = {
225         .driver = {
226                    .name = "pmbus",
227                    },
228         .probe = pmbus_probe,
229         .remove = pmbus_do_remove,
230         .id_table = pmbus_id,
231 };
232
233 module_i2c_driver(pmbus_driver);
234
235 MODULE_AUTHOR("Guenter Roeck");
236 MODULE_DESCRIPTION("Generic PMBus driver");
237 MODULE_LICENSE("GPL");