xref: /linux/drivers/hwmon/pmbus/pmbus.c (revision 1f2367a39f17bd553a75e179a747f9b257bc9478)
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 struct pmbus_device_info {
32 	int pages;
33 	u32 flags;
34 };
35 
36 /*
37  * Find sensor groups and status registers on each page.
38  */
39 static void pmbus_find_sensor_groups(struct i2c_client *client,
40 				     struct pmbus_driver_info *info)
41 {
42 	int page;
43 
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;
53 	if (info->func[0]
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;
61 	}
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;
67 	}
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
75 			     | PMBUS_HAVE_TEMP3)
76 	    && pmbus_check_byte_register(client, 0,
77 					 PMBUS_STATUS_TEMPERATURE))
78 			info->func[0] |= PMBUS_HAVE_STATUS_TEMP;
79 
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,
85 						      PMBUS_STATUS_VOUT))
86 				info->func[page] |= PMBUS_HAVE_STATUS_VOUT;
87 		}
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,
91 						      PMBUS_STATUS_IOUT))
92 				info->func[page] |= PMBUS_HAVE_STATUS_IOUT;
93 		}
94 		if (pmbus_check_word_register(client, page, PMBUS_READ_POUT))
95 			info->func[page] |= PMBUS_HAVE_POUT;
96 	}
97 }
98 
99 /*
100  * Identify chip parameters.
101  */
102 static int pmbus_identify(struct i2c_client *client,
103 			  struct pmbus_driver_info *info)
104 {
105 	int ret = 0;
106 
107 	if (!info->pages) {
108 		/*
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.
113 		 */
114 		if (pmbus_check_byte_register(client, 0, PMBUS_PAGE)) {
115 			int page;
116 
117 			for (page = 1; page < PMBUS_PAGES; page++) {
118 				if (pmbus_set_page(client, page) < 0)
119 					break;
120 			}
121 			pmbus_set_page(client, 0);
122 			info->pages = page;
123 		} else {
124 			info->pages = 1;
125 		}
126 
127 		pmbus_clear_faults(client);
128 	}
129 
130 	if (pmbus_check_byte_register(client, 0, PMBUS_VOUT_MODE)) {
131 		int vout_mode;
132 
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) {
136 			case 0:
137 				break;
138 			case 1:
139 				info->format[PSC_VOLTAGE_OUT] = vid;
140 				info->vrm_version = vr11;
141 				break;
142 			case 2:
143 				info->format[PSC_VOLTAGE_OUT] = direct;
144 				break;
145 			default:
146 				ret = -ENODEV;
147 				goto abort;
148 			}
149 		}
150 	}
151 
152 	/*
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
156 	 * registers.
157 	 *
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
161 	 * mode was detected.
162 	 */
163 	if (info->format[PSC_VOLTAGE_OUT] == direct) {
164 		ret = -ENODEV;
165 		goto abort;
166 	}
167 
168 	/* Try to find sensor groups  */
169 	pmbus_find_sensor_groups(client, info);
170 abort:
171 	return ret;
172 }
173 
174 static int pmbus_probe(struct i2c_client *client,
175 		       const struct i2c_device_id *id)
176 {
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;
181 
182 	info = devm_kzalloc(dev, sizeof(struct pmbus_driver_info), GFP_KERNEL);
183 	if (!info)
184 		return -ENOMEM;
185 
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),
189 				     GFP_KERNEL);
190 		if (!pdata)
191 			return -ENOMEM;
192 
193 		pdata->flags = PMBUS_SKIP_STATUS_CHECK;
194 	}
195 
196 	info->pages = device_info->pages;
197 	info->identify = pmbus_identify;
198 	dev->platform_data = pdata;
199 
200 	return pmbus_do_probe(client, id, info);
201 }
202 
203 static const struct pmbus_device_info pmbus_info_one = {
204 	.pages = 1,
205 	.flags = 0
206 };
207 static const struct pmbus_device_info pmbus_info_zero = {
208 	.pages = 0,
209 	.flags = 0
210 };
211 static const struct pmbus_device_info pmbus_info_one_skip = {
212 	.pages = 1,
213 	.flags = PMBUS_SKIP_STATUS_CHECK
214 };
215 
216 /*
217  * Use driver_data to set the number of pages supported by the chip.
218  */
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},
240 	{}
241 };
242 
243 MODULE_DEVICE_TABLE(i2c, pmbus_id);
244 
245 /* This is the driver that will be inserted */
246 static struct i2c_driver pmbus_driver = {
247 	.driver = {
248 		   .name = "pmbus",
249 		   },
250 	.probe = pmbus_probe,
251 	.remove = pmbus_do_remove,
252 	.id_table = pmbus_id,
253 };
254 
255 module_i2c_driver(pmbus_driver);
256 
257 MODULE_AUTHOR("Guenter Roeck");
258 MODULE_DESCRIPTION("Generic PMBus driver");
259 MODULE_LICENSE("GPL");
260