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