File Index
Files and directories
Sign in
FILE

ucd9000.c

File preview with syntax highlighting.

linux-6.19.14/drivers/hwmon/pmbus/ucd9000.c C
  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
 23enum 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
 63struct 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
 73struct 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
 88static 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
103static 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
144static 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};
154MODULE_DEVICE_TABLE(i2c, ucd9000_id);
155
156static 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};
187MODULE_DEVICE_TABLE(of, ucd9000_of_match);
188
189#ifdef CONFIG_GPIOLIB
190static 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
203static 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
215static int 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 ret;
226	}
227
228	if (value) {
229		if (ret & UCD9000_GPIO_CONFIG_OUT_VALUE)
230			return 0;
231
232		ret |= UCD9000_GPIO_CONFIG_OUT_VALUE;
233	} else {
234		if (!(ret & UCD9000_GPIO_CONFIG_OUT_VALUE))
235			return 0;
236
237		ret &= ~UCD9000_GPIO_CONFIG_OUT_VALUE;
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 ret;
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	return ret;
258}
259
260static int ucd9000_gpio_get_direction(struct gpio_chip *gc,
261				      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_OUT_ENABLE);
271}
272
273static int ucd9000_gpio_set_direction(struct gpio_chip *gc,
274				      unsigned int offset, bool direction_out,
275				      int requested_out)
276{
277	struct i2c_client *client = gpiochip_get_data(gc);
278	int ret, config, out_val;
279
280	ret = ucd9000_gpio_read_config(client, offset);
281	if (ret < 0)
282		return ret;
283
284	if (direction_out) {
285		out_val = requested_out ? UCD9000_GPIO_CONFIG_OUT_VALUE : 0;
286
287		if (ret & UCD9000_GPIO_CONFIG_OUT_ENABLE) {
288			if ((ret & UCD9000_GPIO_CONFIG_OUT_VALUE) == out_val)
289				return 0;
290		} else {
291			ret |= UCD9000_GPIO_CONFIG_OUT_ENABLE;
292		}
293
294		if (out_val)
295			ret |= UCD9000_GPIO_CONFIG_OUT_VALUE;
296		else
297			ret &= ~UCD9000_GPIO_CONFIG_OUT_VALUE;
298
299	} else {
300		if (!(ret & UCD9000_GPIO_CONFIG_OUT_ENABLE))
301			return 0;
302
303		ret &= ~UCD9000_GPIO_CONFIG_OUT_ENABLE;
304	}
305
306	ret |= UCD9000_GPIO_CONFIG_ENABLE;
307	config = ret;
308
309	/* Page set not required */
310	ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, config);
311	if (ret < 0)
312		return ret;
313
314	config &= ~UCD9000_GPIO_CONFIG_ENABLE;
315
316	return i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, config);
317}
318
319static int ucd9000_gpio_direction_input(struct gpio_chip *gc,
320					unsigned int offset)
321{
322	return ucd9000_gpio_set_direction(gc, offset, UCD9000_GPIO_INPUT, 0);
323}
324
325static int ucd9000_gpio_direction_output(struct gpio_chip *gc,
326					 unsigned int offset, int val)
327{
328	return ucd9000_gpio_set_direction(gc, offset, UCD9000_GPIO_OUTPUT,
329					  val);
330}
331
332static void ucd9000_probe_gpio(struct i2c_client *client,
333			       const struct i2c_device_id *mid,
334			       struct ucd9000_data *data)
335{
336	int rc;
337
338	switch (mid->driver_data) {
339	case ucd9090:
340		data->gpio.ngpio = UCD9090_NUM_GPIOS;
341		break;
342	case ucd90120:
343	case ucd90124:
344	case ucd90160:
345		data->gpio.ngpio = UCD901XX_NUM_GPIOS;
346		break;
347	case ucd90320:
348		data->gpio.ngpio = UCD90320_NUM_GPIOS;
349		break;
350	case ucd90910:
351		data->gpio.ngpio = UCD90910_NUM_GPIOS;
352		break;
353	default:
354		return; /* GPIO support is optional. */
355	}
356
357	/*
358	 * Pinmux support has not been added to the new gpio_chip.
359	 * This support should be added when possible given the mux
360	 * behavior of these IO devices.
361	 */
362	data->gpio.label = client->name;
363	data->gpio.get_direction = ucd9000_gpio_get_direction;
364	data->gpio.direction_input = ucd9000_gpio_direction_input;
365	data->gpio.direction_output = ucd9000_gpio_direction_output;
366	data->gpio.get = ucd9000_gpio_get;
367	data->gpio.set = ucd9000_gpio_set;
368	data->gpio.can_sleep = true;
369	data->gpio.base = -1;
370	data->gpio.parent = &client->dev;
371
372	rc = devm_gpiochip_add_data(&client->dev, &data->gpio, client);
373	if (rc)
374		dev_warn(&client->dev, "Could not add gpiochip: %d\n", rc);
375}
376#else
377static void ucd9000_probe_gpio(struct i2c_client *client,
378			       const struct i2c_device_id *mid,
379			       struct ucd9000_data *data)
380{
381}
382#endif /* CONFIG_GPIOLIB */
383
384#ifdef CONFIG_DEBUG_FS
385static int ucd9000_get_mfr_status(struct i2c_client *client, u8 *buffer)
386{
387	int ret = pmbus_set_page(client, 0, 0xff);
388
389	if (ret < 0)
390		return ret;
391
392	return i2c_smbus_read_block_data(client, UCD9000_MFR_STATUS, buffer);
393}
394
395static int ucd9000_debugfs_show_mfr_status_bit(void *data, u64 *val)
396{
397	struct ucd9000_debugfs_entry *entry = data;
398	struct i2c_client *client = entry->client;
399	u8 buffer[I2C_SMBUS_BLOCK_MAX];
400	int ret, i;
401
402	ret = ucd9000_get_mfr_status(client, buffer);
403	if (ret < 0)
404		return ret;
405
406	/*
407	 * GPI fault bits are in sets of 8, two bytes from end of response.
408	 */
409	i = ret - 3 - entry->index / 8;
410	if (i >= 0)
411		*val = !!(buffer[i] & BIT(entry->index % 8));
412
413	return 0;
414}
415DEFINE_DEBUGFS_ATTRIBUTE(ucd9000_debugfs_mfr_status_bit,
416			 ucd9000_debugfs_show_mfr_status_bit, NULL, "%1lld\n");
417
418static ssize_t ucd9000_debugfs_read_mfr_status(struct file *file,
419					       char __user *buf, size_t count,
420					       loff_t *ppos)
421{
422	struct i2c_client *client = file->private_data;
423	u8 buffer[I2C_SMBUS_BLOCK_MAX];
424	char str[(I2C_SMBUS_BLOCK_MAX * 2) + 2];
425	char *res;
426	int rc;
427
428	rc = ucd9000_get_mfr_status(client, buffer);
429	if (rc < 0)
430		return rc;
431
432	res = bin2hex(str, buffer, min(rc, I2C_SMBUS_BLOCK_MAX));
433	*res++ = '\n';
434	*res = 0;
435
436	return simple_read_from_buffer(buf, count, ppos, str, res - str);
437}
438
439static const struct file_operations ucd9000_debugfs_show_mfr_status_fops = {
440	.llseek = noop_llseek,
441	.read = ucd9000_debugfs_read_mfr_status,
442	.open = simple_open,
443};
444
445static int ucd9000_init_debugfs(struct i2c_client *client,
446				const struct i2c_device_id *mid,
447				struct ucd9000_data *data)
448{
449	struct dentry *debugfs;
450	struct ucd9000_debugfs_entry *entries;
451	int i, gpi_count;
452	char name[UCD9000_DEBUGFS_NAME_LEN];
453
454	debugfs = pmbus_get_debugfs_dir(client);
455	if (!debugfs)
456		return -ENOENT;
457
458	data->debugfs = debugfs_create_dir(client->name, debugfs);
459
460	/*
461	 * Of the chips this driver supports, only the UCD9090, UCD90160,
462	 * UCD90320, and UCD90910 report GPI faults in their MFR_STATUS
463	 * register, so only create the GPI fault debugfs attributes for those
464	 * chips.
465	 */
466	if (mid->driver_data == ucd9090 || mid->driver_data == ucd90160 ||
467	    mid->driver_data == ucd90320 || mid->driver_data == ucd90910) {
468		gpi_count = mid->driver_data == ucd90320 ? UCD90320_GPI_COUNT
469							 : UCD9000_GPI_COUNT;
470		entries = devm_kcalloc(&client->dev,
471				       gpi_count, sizeof(*entries),
472				       GFP_KERNEL);
473		if (!entries)
474			return -ENOMEM;
475
476		for (i = 0; i < gpi_count; i++) {
477			entries[i].client = client;
478			entries[i].index = i;
479			scnprintf(name, UCD9000_DEBUGFS_NAME_LEN,
480				  "gpi%d_alarm", i + 1);
481			debugfs_create_file(name, 0444, data->debugfs,
482					    &entries[i],
483					    &ucd9000_debugfs_mfr_status_bit);
484		}
485	}
486
487	scnprintf(name, UCD9000_DEBUGFS_NAME_LEN, "mfr_status");
488	debugfs_create_file(name, 0444, data->debugfs, client,
489			    &ucd9000_debugfs_show_mfr_status_fops);
490
491	return 0;
492}
493#else
494static int ucd9000_init_debugfs(struct i2c_client *client,
495				const struct i2c_device_id *mid,
496				struct ucd9000_data *data)
497{
498	return 0;
499}
500#endif /* CONFIG_DEBUG_FS */
501
502static int ucd9000_probe(struct i2c_client *client)
503{
504	u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
505	struct ucd9000_data *data;
506	struct pmbus_driver_info *info;
507	const struct i2c_device_id *mid;
508	enum chips chip;
509	int i, ret;
510
511	if (!i2c_check_functionality(client->adapter,
512				     I2C_FUNC_SMBUS_BYTE_DATA |
513				     I2C_FUNC_SMBUS_BLOCK_DATA))
514		return -ENODEV;
515
516	ret = i2c_smbus_read_block_data(client, UCD9000_DEVICE_ID,
517					block_buffer);
518	if (ret < 0) {
519		dev_err(&client->dev, "Failed to read device ID\n");
520		return ret;
521	}
522	block_buffer[ret] = '\0';
523	dev_info(&client->dev, "Device ID %s\n", block_buffer);
524
525	for (mid = ucd9000_id; mid->name[0]; mid++) {
526		if (!strncasecmp(mid->name, block_buffer, strlen(mid->name)))
527			break;
528	}
529	if (!mid->name[0]) {
530		dev_err(&client->dev, "Unsupported device\n");
531		return -ENODEV;
532	}
533
534	if (client->dev.of_node)
535		chip = (uintptr_t)of_device_get_match_data(&client->dev);
536	else
537		chip = mid->driver_data;
538
539	if (chip != ucd9000 && strcmp(client->name, mid->name) != 0)
540		dev_notice(&client->dev,
541			   "Device mismatch: Configured %s, detected %s\n",
542			   client->name, mid->name);
543
544	data = devm_kzalloc(&client->dev, sizeof(struct ucd9000_data),
545			    GFP_KERNEL);
546	if (!data)
547		return -ENOMEM;
548	info = &data->info;
549
550	ret = i2c_smbus_read_byte_data(client, UCD9000_NUM_PAGES);
551	if (ret < 0) {
552		dev_err(&client->dev,
553			"Failed to read number of active pages\n");
554		return ret;
555	}
556	info->pages = ret;
557	if (!info->pages) {
558		dev_err(&client->dev, "No pages configured\n");
559		return -ENODEV;
560	}
561
562	/* The internal temperature sensor is always active */
563	info->func[0] = PMBUS_HAVE_TEMP;
564
565	/* Everything else is configurable */
566	ret = i2c_smbus_read_block_data(client, UCD9000_MONITOR_CONFIG,
567					block_buffer);
568	if (ret <= 0) {
569		dev_err(&client->dev, "Failed to read configuration data\n");
570		return -ENODEV;
571	}
572	for (i = 0; i < ret; i++) {
573		int page = UCD9000_MON_PAGE(block_buffer[i]);
574
575		if (page >= info->pages)
576			continue;
577
578		switch (UCD9000_MON_TYPE(block_buffer[i])) {
579		case UCD9000_MON_VOLTAGE:
580		case UCD9000_MON_VOLTAGE_HW:
581			info->func[page] |= PMBUS_HAVE_VOUT
582			  | PMBUS_HAVE_STATUS_VOUT;
583			break;
584		case UCD9000_MON_TEMPERATURE:
585			info->func[page] |= PMBUS_HAVE_TEMP2
586			  | PMBUS_HAVE_STATUS_TEMP;
587			break;
588		case UCD9000_MON_CURRENT:
589			info->func[page] |= PMBUS_HAVE_IOUT
590			  | PMBUS_HAVE_STATUS_IOUT;
591			break;
592		default:
593			break;
594		}
595	}
596
597	/* Fan configuration */
598	if (mid->driver_data == ucd90124) {
599		for (i = 0; i < UCD9000_NUM_FAN; i++) {
600			i2c_smbus_write_byte_data(client,
601						  UCD9000_FAN_CONFIG_INDEX, i);
602			ret = i2c_smbus_read_block_data(client,
603							UCD9000_FAN_CONFIG,
604							data->fan_data[i]);
605			if (ret < 0)
606				return ret;
607		}
608		i2c_smbus_write_byte_data(client, UCD9000_FAN_CONFIG_INDEX, 0);
609
610		info->read_byte_data = ucd9000_read_byte_data;
611		info->func[0] |= PMBUS_HAVE_FAN12 | PMBUS_HAVE_STATUS_FAN12
612		  | PMBUS_HAVE_FAN34 | PMBUS_HAVE_STATUS_FAN34;
613	} else if (mid->driver_data == ucd90320) {
614		/* Delay SMBus operations after a write */
615		info->write_delay = UCD90320_WAIT_DELAY_US;
616	}
617
618	ucd9000_probe_gpio(client, mid, data);
619
620	ret = pmbus_do_probe(client, info);
621	if (ret)
622		return ret;
623
624	ret = ucd9000_init_debugfs(client, mid, data);
625	if (ret)
626		dev_warn(&client->dev, "Failed to register debugfs: %d\n",
627			 ret);
628
629	return 0;
630}
631
632/* This is the driver that will be inserted */
633static struct i2c_driver ucd9000_driver = {
634	.driver = {
635		.name = "ucd9000",
636		.of_match_table = of_match_ptr(ucd9000_of_match),
637	},
638	.probe = ucd9000_probe,
639	.id_table = ucd9000_id,
640};
641
642module_i2c_driver(ucd9000_driver);
643
644MODULE_AUTHOR("Guenter Roeck");
645MODULE_DESCRIPTION("PMBus driver for TI UCD90xxx");
646MODULE_LICENSE("GPL");
647MODULE_IMPORT_NS("PMBUS");
~/ Read-only file preview.