1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Hardware monitoring driver for UCD90xxx Sequencer and System Health
4  * Controller series
5  *
6  * Copyright (C) 2011 Ericsson AB.
7  */
8 
9 #include <linux/debugfs.h>
10 #include <linux/delay.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/of.h>
14 #include <linux/init.h>
15 #include <linux/err.h>
16 #include <linux/slab.h>
17 #include <linux/i2c.h>
18 #include <linux/pmbus.h>
19 #include <linux/gpio/driver.h>
20 #include <linux/timekeeping.h>
21 #include "pmbus.h"
22 
23 enum chips { ucd9000, ucd90120, ucd90124, ucd90160, ucd90320, ucd9090,
24 	     ucd90910 };
25 
26 #define UCD9000_MONITOR_CONFIG		0xd5
27 #define UCD9000_NUM_PAGES		0xd6
28 #define UCD9000_FAN_CONFIG_INDEX	0xe7
29 #define UCD9000_FAN_CONFIG		0xe8
30 #define UCD9000_MFR_STATUS		0xf3
31 #define UCD9000_GPIO_SELECT		0xfa
32 #define UCD9000_GPIO_CONFIG		0xfb
33 #define UCD9000_DEVICE_ID		0xfd
34 
35 /* GPIO CONFIG bits */
36 #define UCD9000_GPIO_CONFIG_ENABLE	BIT(0)
37 #define UCD9000_GPIO_CONFIG_OUT_ENABLE	BIT(1)
38 #define UCD9000_GPIO_CONFIG_OUT_VALUE	BIT(2)
39 #define UCD9000_GPIO_CONFIG_STATUS	BIT(3)
40 #define UCD9000_GPIO_INPUT		0
41 #define UCD9000_GPIO_OUTPUT		1
42 
43 #define UCD9000_MON_TYPE(x)	(((x) >> 5) & 0x07)
44 #define UCD9000_MON_PAGE(x)	((x) & 0x1f)
45 
46 #define UCD9000_MON_VOLTAGE	1
47 #define UCD9000_MON_TEMPERATURE	2
48 #define UCD9000_MON_CURRENT	3
49 #define UCD9000_MON_VOLTAGE_HW	4
50 
51 #define UCD9000_NUM_FAN		4
52 
53 #define UCD9000_GPIO_NAME_LEN	16
54 #define UCD9090_NUM_GPIOS	23
55 #define UCD901XX_NUM_GPIOS	26
56 #define UCD90320_NUM_GPIOS	84
57 #define UCD90910_NUM_GPIOS	26
58 
59 #define UCD9000_DEBUGFS_NAME_LEN	24
60 #define UCD9000_GPI_COUNT		8
61 #define UCD90320_GPI_COUNT		32
62 
63 struct ucd9000_data {
64 	u8 fan_data[UCD9000_NUM_FAN][I2C_SMBUS_BLOCK_MAX];
65 	struct pmbus_driver_info info;
66 #ifdef CONFIG_GPIOLIB
67 	struct gpio_chip gpio;
68 #endif
69 	struct dentry *debugfs;
70 	ktime_t write_time;
71 };
72 #define to_ucd9000_data(_info) container_of(_info, struct ucd9000_data, info)
73 
74 struct ucd9000_debugfs_entry {
75 	struct i2c_client *client;
76 	u8 index;
77 };
78 
79 /*
80  * It has been observed that the UCD90320 randomly fails register access when
81  * doing another access right on the back of a register write. To mitigate this
82  * make sure that there is a minimum delay between a write access and the
83  * following access. The 250us is based on experimental data. At a delay of
84  * 200us the issue seems to go away. Add a bit of extra margin to allow for
85  * system to system differences.
86  */
87 #define UCD90320_WAIT_DELAY_US 250
88 
ucd90320_wait(const struct ucd9000_data * data)89 static inline void ucd90320_wait(const struct ucd9000_data *data)
90 {
91 	s64 delta = ktime_us_delta(ktime_get(), data->write_time);
92 
93 	if (delta < UCD90320_WAIT_DELAY_US)
94 		udelay(UCD90320_WAIT_DELAY_US - delta);
95 }
96 
ucd90320_read_word_data(struct i2c_client * client,int page,int phase,int reg)97 static int ucd90320_read_word_data(struct i2c_client *client, int page,
98 				   int phase, int reg)
99 {
100 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
101 	struct ucd9000_data *data = to_ucd9000_data(info);
102 
103 	if (reg >= PMBUS_VIRT_BASE)
104 		return -ENXIO;
105 
106 	ucd90320_wait(data);
107 	return pmbus_read_word_data(client, page, phase, reg);
108 }
109 
ucd90320_read_byte_data(struct i2c_client * client,int page,int reg)110 static int ucd90320_read_byte_data(struct i2c_client *client, int page, int reg)
111 {
112 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
113 	struct ucd9000_data *data = to_ucd9000_data(info);
114 
115 	ucd90320_wait(data);
116 	return pmbus_read_byte_data(client, page, reg);
117 }
118 
ucd90320_write_word_data(struct i2c_client * client,int page,int reg,u16 word)119 static int ucd90320_write_word_data(struct i2c_client *client, int page,
120 				    int reg, u16 word)
121 {
122 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
123 	struct ucd9000_data *data = to_ucd9000_data(info);
124 	int ret;
125 
126 	ucd90320_wait(data);
127 	ret = pmbus_write_word_data(client, page, reg, word);
128 	data->write_time = ktime_get();
129 
130 	return ret;
131 }
132 
ucd90320_write_byte(struct i2c_client * client,int page,u8 value)133 static int ucd90320_write_byte(struct i2c_client *client, int page, u8 value)
134 {
135 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
136 	struct ucd9000_data *data = to_ucd9000_data(info);
137 	int ret;
138 
139 	ucd90320_wait(data);
140 	ret = pmbus_write_byte(client, page, value);
141 	data->write_time = ktime_get();
142 
143 	return ret;
144 }
145 
ucd9000_get_fan_config(struct i2c_client * client,int fan)146 static int ucd9000_get_fan_config(struct i2c_client *client, int fan)
147 {
148 	int fan_config = 0;
149 	struct ucd9000_data *data
150 	  = to_ucd9000_data(pmbus_get_driver_info(client));
151 
152 	if (data->fan_data[fan][3] & 1)
153 		fan_config |= PB_FAN_2_INSTALLED;   /* Use lower bit position */
154 
155 	/* Pulses/revolution */
156 	fan_config |= (data->fan_data[fan][3] & 0x06) >> 1;
157 
158 	return fan_config;
159 }
160 
ucd9000_read_byte_data(struct i2c_client * client,int page,int reg)161 static int ucd9000_read_byte_data(struct i2c_client *client, int page, int reg)
162 {
163 	int ret = 0;
164 	int fan_config;
165 
166 	switch (reg) {
167 	case PMBUS_FAN_CONFIG_12:
168 		if (page > 0)
169 			return -ENXIO;
170 
171 		ret = ucd9000_get_fan_config(client, 0);
172 		if (ret < 0)
173 			return ret;
174 		fan_config = ret << 4;
175 		ret = ucd9000_get_fan_config(client, 1);
176 		if (ret < 0)
177 			return ret;
178 		fan_config |= ret;
179 		ret = fan_config;
180 		break;
181 	case PMBUS_FAN_CONFIG_34:
182 		if (page > 0)
183 			return -ENXIO;
184 
185 		ret = ucd9000_get_fan_config(client, 2);
186 		if (ret < 0)
187 			return ret;
188 		fan_config = ret << 4;
189 		ret = ucd9000_get_fan_config(client, 3);
190 		if (ret < 0)
191 			return ret;
192 		fan_config |= ret;
193 		ret = fan_config;
194 		break;
195 	default:
196 		ret = -ENODATA;
197 		break;
198 	}
199 	return ret;
200 }
201 
202 static const struct i2c_device_id ucd9000_id[] = {
203 	{"ucd9000", ucd9000},
204 	{"ucd90120", ucd90120},
205 	{"ucd90124", ucd90124},
206 	{"ucd90160", ucd90160},
207 	{"ucd90320", ucd90320},
208 	{"ucd9090", ucd9090},
209 	{"ucd90910", ucd90910},
210 	{}
211 };
212 MODULE_DEVICE_TABLE(i2c, ucd9000_id);
213 
214 static const struct of_device_id __maybe_unused ucd9000_of_match[] = {
215 	{
216 		.compatible = "ti,ucd9000",
217 		.data = (void *)ucd9000
218 	},
219 	{
220 		.compatible = "ti,ucd90120",
221 		.data = (void *)ucd90120
222 	},
223 	{
224 		.compatible = "ti,ucd90124",
225 		.data = (void *)ucd90124
226 	},
227 	{
228 		.compatible = "ti,ucd90160",
229 		.data = (void *)ucd90160
230 	},
231 	{
232 		.compatible = "ti,ucd90320",
233 		.data = (void *)ucd90320
234 	},
235 	{
236 		.compatible = "ti,ucd9090",
237 		.data = (void *)ucd9090
238 	},
239 	{
240 		.compatible = "ti,ucd90910",
241 		.data = (void *)ucd90910
242 	},
243 	{ },
244 };
245 MODULE_DEVICE_TABLE(of, ucd9000_of_match);
246 
247 #ifdef CONFIG_GPIOLIB
ucd9000_gpio_read_config(struct i2c_client * client,unsigned int offset)248 static int ucd9000_gpio_read_config(struct i2c_client *client,
249 				    unsigned int offset)
250 {
251 	int ret;
252 
253 	/* No page set required */
254 	ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_SELECT, offset);
255 	if (ret < 0)
256 		return ret;
257 
258 	return i2c_smbus_read_byte_data(client, UCD9000_GPIO_CONFIG);
259 }
260 
ucd9000_gpio_get(struct gpio_chip * gc,unsigned int offset)261 static int ucd9000_gpio_get(struct gpio_chip *gc, unsigned int offset)
262 {
263 	struct i2c_client *client  = gpiochip_get_data(gc);
264 	int ret;
265 
266 	ret = ucd9000_gpio_read_config(client, offset);
267 	if (ret < 0)
268 		return ret;
269 
270 	return !!(ret & UCD9000_GPIO_CONFIG_STATUS);
271 }
272 
ucd9000_gpio_set(struct gpio_chip * gc,unsigned int offset,int value)273 static void ucd9000_gpio_set(struct gpio_chip *gc, unsigned int offset,
274 			     int value)
275 {
276 	struct i2c_client *client = gpiochip_get_data(gc);
277 	int ret;
278 
279 	ret = ucd9000_gpio_read_config(client, offset);
280 	if (ret < 0) {
281 		dev_dbg(&client->dev, "failed to read GPIO %d config: %d\n",
282 			offset, ret);
283 		return;
284 	}
285 
286 	if (value) {
287 		if (ret & UCD9000_GPIO_CONFIG_STATUS)
288 			return;
289 
290 		ret |= UCD9000_GPIO_CONFIG_STATUS;
291 	} else {
292 		if (!(ret & UCD9000_GPIO_CONFIG_STATUS))
293 			return;
294 
295 		ret &= ~UCD9000_GPIO_CONFIG_STATUS;
296 	}
297 
298 	ret |= UCD9000_GPIO_CONFIG_ENABLE;
299 
300 	/* Page set not required */
301 	ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, ret);
302 	if (ret < 0) {
303 		dev_dbg(&client->dev, "Failed to write GPIO %d config: %d\n",
304 			offset, ret);
305 		return;
306 	}
307 
308 	ret &= ~UCD9000_GPIO_CONFIG_ENABLE;
309 
310 	ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, ret);
311 	if (ret < 0)
312 		dev_dbg(&client->dev, "Failed to write GPIO %d config: %d\n",
313 			offset, ret);
314 }
315 
ucd9000_gpio_get_direction(struct gpio_chip * gc,unsigned int offset)316 static int ucd9000_gpio_get_direction(struct gpio_chip *gc,
317 				      unsigned int offset)
318 {
319 	struct i2c_client *client = gpiochip_get_data(gc);
320 	int ret;
321 
322 	ret = ucd9000_gpio_read_config(client, offset);
323 	if (ret < 0)
324 		return ret;
325 
326 	return !(ret & UCD9000_GPIO_CONFIG_OUT_ENABLE);
327 }
328 
ucd9000_gpio_set_direction(struct gpio_chip * gc,unsigned int offset,bool direction_out,int requested_out)329 static int ucd9000_gpio_set_direction(struct gpio_chip *gc,
330 				      unsigned int offset, bool direction_out,
331 				      int requested_out)
332 {
333 	struct i2c_client *client = gpiochip_get_data(gc);
334 	int ret, config, out_val;
335 
336 	ret = ucd9000_gpio_read_config(client, offset);
337 	if (ret < 0)
338 		return ret;
339 
340 	if (direction_out) {
341 		out_val = requested_out ? UCD9000_GPIO_CONFIG_OUT_VALUE : 0;
342 
343 		if (ret & UCD9000_GPIO_CONFIG_OUT_ENABLE) {
344 			if ((ret & UCD9000_GPIO_CONFIG_OUT_VALUE) == out_val)
345 				return 0;
346 		} else {
347 			ret |= UCD9000_GPIO_CONFIG_OUT_ENABLE;
348 		}
349 
350 		if (out_val)
351 			ret |= UCD9000_GPIO_CONFIG_OUT_VALUE;
352 		else
353 			ret &= ~UCD9000_GPIO_CONFIG_OUT_VALUE;
354 
355 	} else {
356 		if (!(ret & UCD9000_GPIO_CONFIG_OUT_ENABLE))
357 			return 0;
358 
359 		ret &= ~UCD9000_GPIO_CONFIG_OUT_ENABLE;
360 	}
361 
362 	ret |= UCD9000_GPIO_CONFIG_ENABLE;
363 	config = ret;
364 
365 	/* Page set not required */
366 	ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, config);
367 	if (ret < 0)
368 		return ret;
369 
370 	config &= ~UCD9000_GPIO_CONFIG_ENABLE;
371 
372 	return i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, config);
373 }
374 
ucd9000_gpio_direction_input(struct gpio_chip * gc,unsigned int offset)375 static int ucd9000_gpio_direction_input(struct gpio_chip *gc,
376 					unsigned int offset)
377 {
378 	return ucd9000_gpio_set_direction(gc, offset, UCD9000_GPIO_INPUT, 0);
379 }
380 
ucd9000_gpio_direction_output(struct gpio_chip * gc,unsigned int offset,int val)381 static int ucd9000_gpio_direction_output(struct gpio_chip *gc,
382 					 unsigned int offset, int val)
383 {
384 	return ucd9000_gpio_set_direction(gc, offset, UCD9000_GPIO_OUTPUT,
385 					  val);
386 }
387 
ucd9000_probe_gpio(struct i2c_client * client,const struct i2c_device_id * mid,struct ucd9000_data * data)388 static void ucd9000_probe_gpio(struct i2c_client *client,
389 			       const struct i2c_device_id *mid,
390 			       struct ucd9000_data *data)
391 {
392 	int rc;
393 
394 	switch (mid->driver_data) {
395 	case ucd9090:
396 		data->gpio.ngpio = UCD9090_NUM_GPIOS;
397 		break;
398 	case ucd90120:
399 	case ucd90124:
400 	case ucd90160:
401 		data->gpio.ngpio = UCD901XX_NUM_GPIOS;
402 		break;
403 	case ucd90320:
404 		data->gpio.ngpio = UCD90320_NUM_GPIOS;
405 		break;
406 	case ucd90910:
407 		data->gpio.ngpio = UCD90910_NUM_GPIOS;
408 		break;
409 	default:
410 		return; /* GPIO support is optional. */
411 	}
412 
413 	/*
414 	 * Pinmux support has not been added to the new gpio_chip.
415 	 * This support should be added when possible given the mux
416 	 * behavior of these IO devices.
417 	 */
418 	data->gpio.label = client->name;
419 	data->gpio.get_direction = ucd9000_gpio_get_direction;
420 	data->gpio.direction_input = ucd9000_gpio_direction_input;
421 	data->gpio.direction_output = ucd9000_gpio_direction_output;
422 	data->gpio.get = ucd9000_gpio_get;
423 	data->gpio.set = ucd9000_gpio_set;
424 	data->gpio.can_sleep = true;
425 	data->gpio.base = -1;
426 	data->gpio.parent = &client->dev;
427 
428 	rc = devm_gpiochip_add_data(&client->dev, &data->gpio, client);
429 	if (rc)
430 		dev_warn(&client->dev, "Could not add gpiochip: %d\n", rc);
431 }
432 #else
ucd9000_probe_gpio(struct i2c_client * client,const struct i2c_device_id * mid,struct ucd9000_data * data)433 static void ucd9000_probe_gpio(struct i2c_client *client,
434 			       const struct i2c_device_id *mid,
435 			       struct ucd9000_data *data)
436 {
437 }
438 #endif /* CONFIG_GPIOLIB */
439 
440 #ifdef CONFIG_DEBUG_FS
ucd9000_get_mfr_status(struct i2c_client * client,u8 * buffer)441 static int ucd9000_get_mfr_status(struct i2c_client *client, u8 *buffer)
442 {
443 	int ret = pmbus_set_page(client, 0, 0xff);
444 
445 	if (ret < 0)
446 		return ret;
447 
448 	return i2c_smbus_read_block_data(client, UCD9000_MFR_STATUS, buffer);
449 }
450 
ucd9000_debugfs_show_mfr_status_bit(void * data,u64 * val)451 static int ucd9000_debugfs_show_mfr_status_bit(void *data, u64 *val)
452 {
453 	struct ucd9000_debugfs_entry *entry = data;
454 	struct i2c_client *client = entry->client;
455 	u8 buffer[I2C_SMBUS_BLOCK_MAX];
456 	int ret, i;
457 
458 	ret = ucd9000_get_mfr_status(client, buffer);
459 	if (ret < 0)
460 		return ret;
461 
462 	/*
463 	 * GPI fault bits are in sets of 8, two bytes from end of response.
464 	 */
465 	i = ret - 3 - entry->index / 8;
466 	if (i >= 0)
467 		*val = !!(buffer[i] & BIT(entry->index % 8));
468 
469 	return 0;
470 }
471 DEFINE_DEBUGFS_ATTRIBUTE(ucd9000_debugfs_mfr_status_bit,
472 			 ucd9000_debugfs_show_mfr_status_bit, NULL, "%1lld\n");
473 
ucd9000_debugfs_read_mfr_status(struct file * file,char __user * buf,size_t count,loff_t * ppos)474 static ssize_t ucd9000_debugfs_read_mfr_status(struct file *file,
475 					       char __user *buf, size_t count,
476 					       loff_t *ppos)
477 {
478 	struct i2c_client *client = file->private_data;
479 	u8 buffer[I2C_SMBUS_BLOCK_MAX];
480 	char str[(I2C_SMBUS_BLOCK_MAX * 2) + 2];
481 	char *res;
482 	int rc;
483 
484 	rc = ucd9000_get_mfr_status(client, buffer);
485 	if (rc < 0)
486 		return rc;
487 
488 	res = bin2hex(str, buffer, min(rc, I2C_SMBUS_BLOCK_MAX));
489 	*res++ = '\n';
490 	*res = 0;
491 
492 	return simple_read_from_buffer(buf, count, ppos, str, res - str);
493 }
494 
495 static const struct file_operations ucd9000_debugfs_show_mfr_status_fops = {
496 	.llseek = noop_llseek,
497 	.read = ucd9000_debugfs_read_mfr_status,
498 	.open = simple_open,
499 };
500 
ucd9000_init_debugfs(struct i2c_client * client,const struct i2c_device_id * mid,struct ucd9000_data * data)501 static int ucd9000_init_debugfs(struct i2c_client *client,
502 				const struct i2c_device_id *mid,
503 				struct ucd9000_data *data)
504 {
505 	struct dentry *debugfs;
506 	struct ucd9000_debugfs_entry *entries;
507 	int i, gpi_count;
508 	char name[UCD9000_DEBUGFS_NAME_LEN];
509 
510 	debugfs = pmbus_get_debugfs_dir(client);
511 	if (!debugfs)
512 		return -ENOENT;
513 
514 	data->debugfs = debugfs_create_dir(client->name, debugfs);
515 
516 	/*
517 	 * Of the chips this driver supports, only the UCD9090, UCD90160,
518 	 * UCD90320, and UCD90910 report GPI faults in their MFR_STATUS
519 	 * register, so only create the GPI fault debugfs attributes for those
520 	 * chips.
521 	 */
522 	if (mid->driver_data == ucd9090 || mid->driver_data == ucd90160 ||
523 	    mid->driver_data == ucd90320 || mid->driver_data == ucd90910) {
524 		gpi_count = mid->driver_data == ucd90320 ? UCD90320_GPI_COUNT
525 							 : UCD9000_GPI_COUNT;
526 		entries = devm_kcalloc(&client->dev,
527 				       gpi_count, sizeof(*entries),
528 				       GFP_KERNEL);
529 		if (!entries)
530 			return -ENOMEM;
531 
532 		for (i = 0; i < gpi_count; i++) {
533 			entries[i].client = client;
534 			entries[i].index = i;
535 			scnprintf(name, UCD9000_DEBUGFS_NAME_LEN,
536 				  "gpi%d_alarm", i + 1);
537 			debugfs_create_file(name, 0444, data->debugfs,
538 					    &entries[i],
539 					    &ucd9000_debugfs_mfr_status_bit);
540 		}
541 	}
542 
543 	scnprintf(name, UCD9000_DEBUGFS_NAME_LEN, "mfr_status");
544 	debugfs_create_file(name, 0444, data->debugfs, client,
545 			    &ucd9000_debugfs_show_mfr_status_fops);
546 
547 	return 0;
548 }
549 #else
ucd9000_init_debugfs(struct i2c_client * client,const struct i2c_device_id * mid,struct ucd9000_data * data)550 static int ucd9000_init_debugfs(struct i2c_client *client,
551 				const struct i2c_device_id *mid,
552 				struct ucd9000_data *data)
553 {
554 	return 0;
555 }
556 #endif /* CONFIG_DEBUG_FS */
557 
ucd9000_probe(struct i2c_client * client)558 static int ucd9000_probe(struct i2c_client *client)
559 {
560 	u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
561 	struct ucd9000_data *data;
562 	struct pmbus_driver_info *info;
563 	const struct i2c_device_id *mid;
564 	enum chips chip;
565 	int i, ret;
566 
567 	if (!i2c_check_functionality(client->adapter,
568 				     I2C_FUNC_SMBUS_BYTE_DATA |
569 				     I2C_FUNC_SMBUS_BLOCK_DATA))
570 		return -ENODEV;
571 
572 	ret = i2c_smbus_read_block_data(client, UCD9000_DEVICE_ID,
573 					block_buffer);
574 	if (ret < 0) {
575 		dev_err(&client->dev, "Failed to read device ID\n");
576 		return ret;
577 	}
578 	block_buffer[ret] = '\0';
579 	dev_info(&client->dev, "Device ID %s\n", block_buffer);
580 
581 	for (mid = ucd9000_id; mid->name[0]; mid++) {
582 		if (!strncasecmp(mid->name, block_buffer, strlen(mid->name)))
583 			break;
584 	}
585 	if (!mid->name[0]) {
586 		dev_err(&client->dev, "Unsupported device\n");
587 		return -ENODEV;
588 	}
589 
590 	if (client->dev.of_node)
591 		chip = (uintptr_t)of_device_get_match_data(&client->dev);
592 	else
593 		chip = mid->driver_data;
594 
595 	if (chip != ucd9000 && strcmp(client->name, mid->name) != 0)
596 		dev_notice(&client->dev,
597 			   "Device mismatch: Configured %s, detected %s\n",
598 			   client->name, mid->name);
599 
600 	data = devm_kzalloc(&client->dev, sizeof(struct ucd9000_data),
601 			    GFP_KERNEL);
602 	if (!data)
603 		return -ENOMEM;
604 	info = &data->info;
605 
606 	ret = i2c_smbus_read_byte_data(client, UCD9000_NUM_PAGES);
607 	if (ret < 0) {
608 		dev_err(&client->dev,
609 			"Failed to read number of active pages\n");
610 		return ret;
611 	}
612 	info->pages = ret;
613 	if (!info->pages) {
614 		dev_err(&client->dev, "No pages configured\n");
615 		return -ENODEV;
616 	}
617 
618 	/* The internal temperature sensor is always active */
619 	info->func[0] = PMBUS_HAVE_TEMP;
620 
621 	/* Everything else is configurable */
622 	ret = i2c_smbus_read_block_data(client, UCD9000_MONITOR_CONFIG,
623 					block_buffer);
624 	if (ret <= 0) {
625 		dev_err(&client->dev, "Failed to read configuration data\n");
626 		return -ENODEV;
627 	}
628 	for (i = 0; i < ret; i++) {
629 		int page = UCD9000_MON_PAGE(block_buffer[i]);
630 
631 		if (page >= info->pages)
632 			continue;
633 
634 		switch (UCD9000_MON_TYPE(block_buffer[i])) {
635 		case UCD9000_MON_VOLTAGE:
636 		case UCD9000_MON_VOLTAGE_HW:
637 			info->func[page] |= PMBUS_HAVE_VOUT
638 			  | PMBUS_HAVE_STATUS_VOUT;
639 			break;
640 		case UCD9000_MON_TEMPERATURE:
641 			info->func[page] |= PMBUS_HAVE_TEMP2
642 			  | PMBUS_HAVE_STATUS_TEMP;
643 			break;
644 		case UCD9000_MON_CURRENT:
645 			info->func[page] |= PMBUS_HAVE_IOUT
646 			  | PMBUS_HAVE_STATUS_IOUT;
647 			break;
648 		default:
649 			break;
650 		}
651 	}
652 
653 	/* Fan configuration */
654 	if (mid->driver_data == ucd90124) {
655 		for (i = 0; i < UCD9000_NUM_FAN; i++) {
656 			i2c_smbus_write_byte_data(client,
657 						  UCD9000_FAN_CONFIG_INDEX, i);
658 			ret = i2c_smbus_read_block_data(client,
659 							UCD9000_FAN_CONFIG,
660 							data->fan_data[i]);
661 			if (ret < 0)
662 				return ret;
663 		}
664 		i2c_smbus_write_byte_data(client, UCD9000_FAN_CONFIG_INDEX, 0);
665 
666 		info->read_byte_data = ucd9000_read_byte_data;
667 		info->func[0] |= PMBUS_HAVE_FAN12 | PMBUS_HAVE_STATUS_FAN12
668 		  | PMBUS_HAVE_FAN34 | PMBUS_HAVE_STATUS_FAN34;
669 	} else if (mid->driver_data == ucd90320) {
670 		info->read_byte_data = ucd90320_read_byte_data;
671 		info->read_word_data = ucd90320_read_word_data;
672 		info->write_byte = ucd90320_write_byte;
673 		info->write_word_data = ucd90320_write_word_data;
674 	}
675 
676 	ucd9000_probe_gpio(client, mid, data);
677 
678 	ret = pmbus_do_probe(client, info);
679 	if (ret)
680 		return ret;
681 
682 	ret = ucd9000_init_debugfs(client, mid, data);
683 	if (ret)
684 		dev_warn(&client->dev, "Failed to register debugfs: %d\n",
685 			 ret);
686 
687 	return 0;
688 }
689 
690 /* This is the driver that will be inserted */
691 static struct i2c_driver ucd9000_driver = {
692 	.driver = {
693 		.name = "ucd9000",
694 		.of_match_table = of_match_ptr(ucd9000_of_match),
695 	},
696 	.probe = ucd9000_probe,
697 	.id_table = ucd9000_id,
698 };
699 
700 module_i2c_driver(ucd9000_driver);
701 
702 MODULE_AUTHOR("Guenter Roeck");
703 MODULE_DESCRIPTION("PMBus driver for TI UCD90xxx");
704 MODULE_LICENSE("GPL");
705 MODULE_IMPORT_NS(PMBUS);
706