18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Hardware monitoring driver for UCD90xxx Sequencer and System Health
48c2ecf20Sopenharmony_ci * Controller series
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (C) 2011 Ericsson AB.
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/debugfs.h>
108c2ecf20Sopenharmony_ci#include <linux/delay.h>
118c2ecf20Sopenharmony_ci#include <linux/kernel.h>
128c2ecf20Sopenharmony_ci#include <linux/module.h>
138c2ecf20Sopenharmony_ci#include <linux/of_device.h>
148c2ecf20Sopenharmony_ci#include <linux/init.h>
158c2ecf20Sopenharmony_ci#include <linux/err.h>
168c2ecf20Sopenharmony_ci#include <linux/slab.h>
178c2ecf20Sopenharmony_ci#include <linux/i2c.h>
188c2ecf20Sopenharmony_ci#include <linux/pmbus.h>
198c2ecf20Sopenharmony_ci#include <linux/gpio/driver.h>
208c2ecf20Sopenharmony_ci#include <linux/timekeeping.h>
218c2ecf20Sopenharmony_ci#include "pmbus.h"
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cienum chips { ucd9000, ucd90120, ucd90124, ucd90160, ucd90320, ucd9090,
248c2ecf20Sopenharmony_ci	     ucd90910 };
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define UCD9000_MONITOR_CONFIG		0xd5
278c2ecf20Sopenharmony_ci#define UCD9000_NUM_PAGES		0xd6
288c2ecf20Sopenharmony_ci#define UCD9000_FAN_CONFIG_INDEX	0xe7
298c2ecf20Sopenharmony_ci#define UCD9000_FAN_CONFIG		0xe8
308c2ecf20Sopenharmony_ci#define UCD9000_MFR_STATUS		0xf3
318c2ecf20Sopenharmony_ci#define UCD9000_GPIO_SELECT		0xfa
328c2ecf20Sopenharmony_ci#define UCD9000_GPIO_CONFIG		0xfb
338c2ecf20Sopenharmony_ci#define UCD9000_DEVICE_ID		0xfd
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/* GPIO CONFIG bits */
368c2ecf20Sopenharmony_ci#define UCD9000_GPIO_CONFIG_ENABLE	BIT(0)
378c2ecf20Sopenharmony_ci#define UCD9000_GPIO_CONFIG_OUT_ENABLE	BIT(1)
388c2ecf20Sopenharmony_ci#define UCD9000_GPIO_CONFIG_OUT_VALUE	BIT(2)
398c2ecf20Sopenharmony_ci#define UCD9000_GPIO_CONFIG_STATUS	BIT(3)
408c2ecf20Sopenharmony_ci#define UCD9000_GPIO_INPUT		0
418c2ecf20Sopenharmony_ci#define UCD9000_GPIO_OUTPUT		1
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci#define UCD9000_MON_TYPE(x)	(((x) >> 5) & 0x07)
448c2ecf20Sopenharmony_ci#define UCD9000_MON_PAGE(x)	((x) & 0x1f)
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci#define UCD9000_MON_VOLTAGE	1
478c2ecf20Sopenharmony_ci#define UCD9000_MON_TEMPERATURE	2
488c2ecf20Sopenharmony_ci#define UCD9000_MON_CURRENT	3
498c2ecf20Sopenharmony_ci#define UCD9000_MON_VOLTAGE_HW	4
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci#define UCD9000_NUM_FAN		4
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#define UCD9000_GPIO_NAME_LEN	16
548c2ecf20Sopenharmony_ci#define UCD9090_NUM_GPIOS	23
558c2ecf20Sopenharmony_ci#define UCD901XX_NUM_GPIOS	26
568c2ecf20Sopenharmony_ci#define UCD90320_NUM_GPIOS	84
578c2ecf20Sopenharmony_ci#define UCD90910_NUM_GPIOS	26
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci#define UCD9000_DEBUGFS_NAME_LEN	24
608c2ecf20Sopenharmony_ci#define UCD9000_GPI_COUNT		8
618c2ecf20Sopenharmony_ci#define UCD90320_GPI_COUNT		32
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistruct ucd9000_data {
648c2ecf20Sopenharmony_ci	u8 fan_data[UCD9000_NUM_FAN][I2C_SMBUS_BLOCK_MAX];
658c2ecf20Sopenharmony_ci	struct pmbus_driver_info info;
668c2ecf20Sopenharmony_ci#ifdef CONFIG_GPIOLIB
678c2ecf20Sopenharmony_ci	struct gpio_chip gpio;
688c2ecf20Sopenharmony_ci#endif
698c2ecf20Sopenharmony_ci	struct dentry *debugfs;
708c2ecf20Sopenharmony_ci	ktime_t write_time;
718c2ecf20Sopenharmony_ci};
728c2ecf20Sopenharmony_ci#define to_ucd9000_data(_info) container_of(_info, struct ucd9000_data, info)
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistruct ucd9000_debugfs_entry {
758c2ecf20Sopenharmony_ci	struct i2c_client *client;
768c2ecf20Sopenharmony_ci	u8 index;
778c2ecf20Sopenharmony_ci};
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci/*
808c2ecf20Sopenharmony_ci * It has been observed that the UCD90320 randomly fails register access when
818c2ecf20Sopenharmony_ci * doing another access right on the back of a register write. To mitigate this
828c2ecf20Sopenharmony_ci * make sure that there is a minimum delay between a write access and the
838c2ecf20Sopenharmony_ci * following access. The 250us is based on experimental data. At a delay of
848c2ecf20Sopenharmony_ci * 200us the issue seems to go away. Add a bit of extra margin to allow for
858c2ecf20Sopenharmony_ci * system to system differences.
868c2ecf20Sopenharmony_ci */
878c2ecf20Sopenharmony_ci#define UCD90320_WAIT_DELAY_US 250
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_cistatic inline void ucd90320_wait(const struct ucd9000_data *data)
908c2ecf20Sopenharmony_ci{
918c2ecf20Sopenharmony_ci	s64 delta = ktime_us_delta(ktime_get(), data->write_time);
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	if (delta < UCD90320_WAIT_DELAY_US)
948c2ecf20Sopenharmony_ci		udelay(UCD90320_WAIT_DELAY_US - delta);
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_cistatic int ucd90320_read_word_data(struct i2c_client *client, int page,
988c2ecf20Sopenharmony_ci				   int phase, int reg)
998c2ecf20Sopenharmony_ci{
1008c2ecf20Sopenharmony_ci	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
1018c2ecf20Sopenharmony_ci	struct ucd9000_data *data = to_ucd9000_data(info);
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	if (reg >= PMBUS_VIRT_BASE)
1048c2ecf20Sopenharmony_ci		return -ENXIO;
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	ucd90320_wait(data);
1078c2ecf20Sopenharmony_ci	return pmbus_read_word_data(client, page, phase, reg);
1088c2ecf20Sopenharmony_ci}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_cistatic int ucd90320_read_byte_data(struct i2c_client *client, int page, int reg)
1118c2ecf20Sopenharmony_ci{
1128c2ecf20Sopenharmony_ci	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
1138c2ecf20Sopenharmony_ci	struct ucd9000_data *data = to_ucd9000_data(info);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	ucd90320_wait(data);
1168c2ecf20Sopenharmony_ci	return pmbus_read_byte_data(client, page, reg);
1178c2ecf20Sopenharmony_ci}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistatic int ucd90320_write_word_data(struct i2c_client *client, int page,
1208c2ecf20Sopenharmony_ci				    int reg, u16 word)
1218c2ecf20Sopenharmony_ci{
1228c2ecf20Sopenharmony_ci	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
1238c2ecf20Sopenharmony_ci	struct ucd9000_data *data = to_ucd9000_data(info);
1248c2ecf20Sopenharmony_ci	int ret;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	ucd90320_wait(data);
1278c2ecf20Sopenharmony_ci	ret = pmbus_write_word_data(client, page, reg, word);
1288c2ecf20Sopenharmony_ci	data->write_time = ktime_get();
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	return ret;
1318c2ecf20Sopenharmony_ci}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_cistatic int ucd90320_write_byte(struct i2c_client *client, int page, u8 value)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
1368c2ecf20Sopenharmony_ci	struct ucd9000_data *data = to_ucd9000_data(info);
1378c2ecf20Sopenharmony_ci	int ret;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	ucd90320_wait(data);
1408c2ecf20Sopenharmony_ci	ret = pmbus_write_byte(client, page, value);
1418c2ecf20Sopenharmony_ci	data->write_time = ktime_get();
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	return ret;
1448c2ecf20Sopenharmony_ci}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_cistatic int ucd9000_get_fan_config(struct i2c_client *client, int fan)
1478c2ecf20Sopenharmony_ci{
1488c2ecf20Sopenharmony_ci	int fan_config = 0;
1498c2ecf20Sopenharmony_ci	struct ucd9000_data *data
1508c2ecf20Sopenharmony_ci	  = to_ucd9000_data(pmbus_get_driver_info(client));
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	if (data->fan_data[fan][3] & 1)
1538c2ecf20Sopenharmony_ci		fan_config |= PB_FAN_2_INSTALLED;   /* Use lower bit position */
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	/* Pulses/revolution */
1568c2ecf20Sopenharmony_ci	fan_config |= (data->fan_data[fan][3] & 0x06) >> 1;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	return fan_config;
1598c2ecf20Sopenharmony_ci}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_cistatic int ucd9000_read_byte_data(struct i2c_client *client, int page, int reg)
1628c2ecf20Sopenharmony_ci{
1638c2ecf20Sopenharmony_ci	int ret = 0;
1648c2ecf20Sopenharmony_ci	int fan_config;
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	switch (reg) {
1678c2ecf20Sopenharmony_ci	case PMBUS_FAN_CONFIG_12:
1688c2ecf20Sopenharmony_ci		if (page > 0)
1698c2ecf20Sopenharmony_ci			return -ENXIO;
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci		ret = ucd9000_get_fan_config(client, 0);
1728c2ecf20Sopenharmony_ci		if (ret < 0)
1738c2ecf20Sopenharmony_ci			return ret;
1748c2ecf20Sopenharmony_ci		fan_config = ret << 4;
1758c2ecf20Sopenharmony_ci		ret = ucd9000_get_fan_config(client, 1);
1768c2ecf20Sopenharmony_ci		if (ret < 0)
1778c2ecf20Sopenharmony_ci			return ret;
1788c2ecf20Sopenharmony_ci		fan_config |= ret;
1798c2ecf20Sopenharmony_ci		ret = fan_config;
1808c2ecf20Sopenharmony_ci		break;
1818c2ecf20Sopenharmony_ci	case PMBUS_FAN_CONFIG_34:
1828c2ecf20Sopenharmony_ci		if (page > 0)
1838c2ecf20Sopenharmony_ci			return -ENXIO;
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci		ret = ucd9000_get_fan_config(client, 2);
1868c2ecf20Sopenharmony_ci		if (ret < 0)
1878c2ecf20Sopenharmony_ci			return ret;
1888c2ecf20Sopenharmony_ci		fan_config = ret << 4;
1898c2ecf20Sopenharmony_ci		ret = ucd9000_get_fan_config(client, 3);
1908c2ecf20Sopenharmony_ci		if (ret < 0)
1918c2ecf20Sopenharmony_ci			return ret;
1928c2ecf20Sopenharmony_ci		fan_config |= ret;
1938c2ecf20Sopenharmony_ci		ret = fan_config;
1948c2ecf20Sopenharmony_ci		break;
1958c2ecf20Sopenharmony_ci	default:
1968c2ecf20Sopenharmony_ci		ret = -ENODATA;
1978c2ecf20Sopenharmony_ci		break;
1988c2ecf20Sopenharmony_ci	}
1998c2ecf20Sopenharmony_ci	return ret;
2008c2ecf20Sopenharmony_ci}
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_cistatic const struct i2c_device_id ucd9000_id[] = {
2038c2ecf20Sopenharmony_ci	{"ucd9000", ucd9000},
2048c2ecf20Sopenharmony_ci	{"ucd90120", ucd90120},
2058c2ecf20Sopenharmony_ci	{"ucd90124", ucd90124},
2068c2ecf20Sopenharmony_ci	{"ucd90160", ucd90160},
2078c2ecf20Sopenharmony_ci	{"ucd90320", ucd90320},
2088c2ecf20Sopenharmony_ci	{"ucd9090", ucd9090},
2098c2ecf20Sopenharmony_ci	{"ucd90910", ucd90910},
2108c2ecf20Sopenharmony_ci	{}
2118c2ecf20Sopenharmony_ci};
2128c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, ucd9000_id);
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_cistatic const struct of_device_id __maybe_unused ucd9000_of_match[] = {
2158c2ecf20Sopenharmony_ci	{
2168c2ecf20Sopenharmony_ci		.compatible = "ti,ucd9000",
2178c2ecf20Sopenharmony_ci		.data = (void *)ucd9000
2188c2ecf20Sopenharmony_ci	},
2198c2ecf20Sopenharmony_ci	{
2208c2ecf20Sopenharmony_ci		.compatible = "ti,ucd90120",
2218c2ecf20Sopenharmony_ci		.data = (void *)ucd90120
2228c2ecf20Sopenharmony_ci	},
2238c2ecf20Sopenharmony_ci	{
2248c2ecf20Sopenharmony_ci		.compatible = "ti,ucd90124",
2258c2ecf20Sopenharmony_ci		.data = (void *)ucd90124
2268c2ecf20Sopenharmony_ci	},
2278c2ecf20Sopenharmony_ci	{
2288c2ecf20Sopenharmony_ci		.compatible = "ti,ucd90160",
2298c2ecf20Sopenharmony_ci		.data = (void *)ucd90160
2308c2ecf20Sopenharmony_ci	},
2318c2ecf20Sopenharmony_ci	{
2328c2ecf20Sopenharmony_ci		.compatible = "ti,ucd90320",
2338c2ecf20Sopenharmony_ci		.data = (void *)ucd90320
2348c2ecf20Sopenharmony_ci	},
2358c2ecf20Sopenharmony_ci	{
2368c2ecf20Sopenharmony_ci		.compatible = "ti,ucd9090",
2378c2ecf20Sopenharmony_ci		.data = (void *)ucd9090
2388c2ecf20Sopenharmony_ci	},
2398c2ecf20Sopenharmony_ci	{
2408c2ecf20Sopenharmony_ci		.compatible = "ti,ucd90910",
2418c2ecf20Sopenharmony_ci		.data = (void *)ucd90910
2428c2ecf20Sopenharmony_ci	},
2438c2ecf20Sopenharmony_ci	{ },
2448c2ecf20Sopenharmony_ci};
2458c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, ucd9000_of_match);
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci#ifdef CONFIG_GPIOLIB
2488c2ecf20Sopenharmony_cistatic int ucd9000_gpio_read_config(struct i2c_client *client,
2498c2ecf20Sopenharmony_ci				    unsigned int offset)
2508c2ecf20Sopenharmony_ci{
2518c2ecf20Sopenharmony_ci	int ret;
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	/* No page set required */
2548c2ecf20Sopenharmony_ci	ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_SELECT, offset);
2558c2ecf20Sopenharmony_ci	if (ret < 0)
2568c2ecf20Sopenharmony_ci		return ret;
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	return i2c_smbus_read_byte_data(client, UCD9000_GPIO_CONFIG);
2598c2ecf20Sopenharmony_ci}
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_cistatic int ucd9000_gpio_get(struct gpio_chip *gc, unsigned int offset)
2628c2ecf20Sopenharmony_ci{
2638c2ecf20Sopenharmony_ci	struct i2c_client *client  = gpiochip_get_data(gc);
2648c2ecf20Sopenharmony_ci	int ret;
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	ret = ucd9000_gpio_read_config(client, offset);
2678c2ecf20Sopenharmony_ci	if (ret < 0)
2688c2ecf20Sopenharmony_ci		return ret;
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	return !!(ret & UCD9000_GPIO_CONFIG_STATUS);
2718c2ecf20Sopenharmony_ci}
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_cistatic void ucd9000_gpio_set(struct gpio_chip *gc, unsigned int offset,
2748c2ecf20Sopenharmony_ci			     int value)
2758c2ecf20Sopenharmony_ci{
2768c2ecf20Sopenharmony_ci	struct i2c_client *client = gpiochip_get_data(gc);
2778c2ecf20Sopenharmony_ci	int ret;
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	ret = ucd9000_gpio_read_config(client, offset);
2808c2ecf20Sopenharmony_ci	if (ret < 0) {
2818c2ecf20Sopenharmony_ci		dev_dbg(&client->dev, "failed to read GPIO %d config: %d\n",
2828c2ecf20Sopenharmony_ci			offset, ret);
2838c2ecf20Sopenharmony_ci		return;
2848c2ecf20Sopenharmony_ci	}
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	if (value) {
2878c2ecf20Sopenharmony_ci		if (ret & UCD9000_GPIO_CONFIG_STATUS)
2888c2ecf20Sopenharmony_ci			return;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci		ret |= UCD9000_GPIO_CONFIG_STATUS;
2918c2ecf20Sopenharmony_ci	} else {
2928c2ecf20Sopenharmony_ci		if (!(ret & UCD9000_GPIO_CONFIG_STATUS))
2938c2ecf20Sopenharmony_ci			return;
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci		ret &= ~UCD9000_GPIO_CONFIG_STATUS;
2968c2ecf20Sopenharmony_ci	}
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	ret |= UCD9000_GPIO_CONFIG_ENABLE;
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	/* Page set not required */
3018c2ecf20Sopenharmony_ci	ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, ret);
3028c2ecf20Sopenharmony_ci	if (ret < 0) {
3038c2ecf20Sopenharmony_ci		dev_dbg(&client->dev, "Failed to write GPIO %d config: %d\n",
3048c2ecf20Sopenharmony_ci			offset, ret);
3058c2ecf20Sopenharmony_ci		return;
3068c2ecf20Sopenharmony_ci	}
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	ret &= ~UCD9000_GPIO_CONFIG_ENABLE;
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, ret);
3118c2ecf20Sopenharmony_ci	if (ret < 0)
3128c2ecf20Sopenharmony_ci		dev_dbg(&client->dev, "Failed to write GPIO %d config: %d\n",
3138c2ecf20Sopenharmony_ci			offset, ret);
3148c2ecf20Sopenharmony_ci}
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_cistatic int ucd9000_gpio_get_direction(struct gpio_chip *gc,
3178c2ecf20Sopenharmony_ci				      unsigned int offset)
3188c2ecf20Sopenharmony_ci{
3198c2ecf20Sopenharmony_ci	struct i2c_client *client = gpiochip_get_data(gc);
3208c2ecf20Sopenharmony_ci	int ret;
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci	ret = ucd9000_gpio_read_config(client, offset);
3238c2ecf20Sopenharmony_ci	if (ret < 0)
3248c2ecf20Sopenharmony_ci		return ret;
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	return !(ret & UCD9000_GPIO_CONFIG_OUT_ENABLE);
3278c2ecf20Sopenharmony_ci}
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_cistatic int ucd9000_gpio_set_direction(struct gpio_chip *gc,
3308c2ecf20Sopenharmony_ci				      unsigned int offset, bool direction_out,
3318c2ecf20Sopenharmony_ci				      int requested_out)
3328c2ecf20Sopenharmony_ci{
3338c2ecf20Sopenharmony_ci	struct i2c_client *client = gpiochip_get_data(gc);
3348c2ecf20Sopenharmony_ci	int ret, config, out_val;
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	ret = ucd9000_gpio_read_config(client, offset);
3378c2ecf20Sopenharmony_ci	if (ret < 0)
3388c2ecf20Sopenharmony_ci		return ret;
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	if (direction_out) {
3418c2ecf20Sopenharmony_ci		out_val = requested_out ? UCD9000_GPIO_CONFIG_OUT_VALUE : 0;
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci		if (ret & UCD9000_GPIO_CONFIG_OUT_ENABLE) {
3448c2ecf20Sopenharmony_ci			if ((ret & UCD9000_GPIO_CONFIG_OUT_VALUE) == out_val)
3458c2ecf20Sopenharmony_ci				return 0;
3468c2ecf20Sopenharmony_ci		} else {
3478c2ecf20Sopenharmony_ci			ret |= UCD9000_GPIO_CONFIG_OUT_ENABLE;
3488c2ecf20Sopenharmony_ci		}
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci		if (out_val)
3518c2ecf20Sopenharmony_ci			ret |= UCD9000_GPIO_CONFIG_OUT_VALUE;
3528c2ecf20Sopenharmony_ci		else
3538c2ecf20Sopenharmony_ci			ret &= ~UCD9000_GPIO_CONFIG_OUT_VALUE;
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	} else {
3568c2ecf20Sopenharmony_ci		if (!(ret & UCD9000_GPIO_CONFIG_OUT_ENABLE))
3578c2ecf20Sopenharmony_ci			return 0;
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci		ret &= ~UCD9000_GPIO_CONFIG_OUT_ENABLE;
3608c2ecf20Sopenharmony_ci	}
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	ret |= UCD9000_GPIO_CONFIG_ENABLE;
3638c2ecf20Sopenharmony_ci	config = ret;
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	/* Page set not required */
3668c2ecf20Sopenharmony_ci	ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, config);
3678c2ecf20Sopenharmony_ci	if (ret < 0)
3688c2ecf20Sopenharmony_ci		return ret;
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	config &= ~UCD9000_GPIO_CONFIG_ENABLE;
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	return i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, config);
3738c2ecf20Sopenharmony_ci}
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_cistatic int ucd9000_gpio_direction_input(struct gpio_chip *gc,
3768c2ecf20Sopenharmony_ci					unsigned int offset)
3778c2ecf20Sopenharmony_ci{
3788c2ecf20Sopenharmony_ci	return ucd9000_gpio_set_direction(gc, offset, UCD9000_GPIO_INPUT, 0);
3798c2ecf20Sopenharmony_ci}
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_cistatic int ucd9000_gpio_direction_output(struct gpio_chip *gc,
3828c2ecf20Sopenharmony_ci					 unsigned int offset, int val)
3838c2ecf20Sopenharmony_ci{
3848c2ecf20Sopenharmony_ci	return ucd9000_gpio_set_direction(gc, offset, UCD9000_GPIO_OUTPUT,
3858c2ecf20Sopenharmony_ci					  val);
3868c2ecf20Sopenharmony_ci}
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_cistatic void ucd9000_probe_gpio(struct i2c_client *client,
3898c2ecf20Sopenharmony_ci			       const struct i2c_device_id *mid,
3908c2ecf20Sopenharmony_ci			       struct ucd9000_data *data)
3918c2ecf20Sopenharmony_ci{
3928c2ecf20Sopenharmony_ci	int rc;
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	switch (mid->driver_data) {
3958c2ecf20Sopenharmony_ci	case ucd9090:
3968c2ecf20Sopenharmony_ci		data->gpio.ngpio = UCD9090_NUM_GPIOS;
3978c2ecf20Sopenharmony_ci		break;
3988c2ecf20Sopenharmony_ci	case ucd90120:
3998c2ecf20Sopenharmony_ci	case ucd90124:
4008c2ecf20Sopenharmony_ci	case ucd90160:
4018c2ecf20Sopenharmony_ci		data->gpio.ngpio = UCD901XX_NUM_GPIOS;
4028c2ecf20Sopenharmony_ci		break;
4038c2ecf20Sopenharmony_ci	case ucd90320:
4048c2ecf20Sopenharmony_ci		data->gpio.ngpio = UCD90320_NUM_GPIOS;
4058c2ecf20Sopenharmony_ci		break;
4068c2ecf20Sopenharmony_ci	case ucd90910:
4078c2ecf20Sopenharmony_ci		data->gpio.ngpio = UCD90910_NUM_GPIOS;
4088c2ecf20Sopenharmony_ci		break;
4098c2ecf20Sopenharmony_ci	default:
4108c2ecf20Sopenharmony_ci		return; /* GPIO support is optional. */
4118c2ecf20Sopenharmony_ci	}
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	/*
4148c2ecf20Sopenharmony_ci	 * Pinmux support has not been added to the new gpio_chip.
4158c2ecf20Sopenharmony_ci	 * This support should be added when possible given the mux
4168c2ecf20Sopenharmony_ci	 * behavior of these IO devices.
4178c2ecf20Sopenharmony_ci	 */
4188c2ecf20Sopenharmony_ci	data->gpio.label = client->name;
4198c2ecf20Sopenharmony_ci	data->gpio.get_direction = ucd9000_gpio_get_direction;
4208c2ecf20Sopenharmony_ci	data->gpio.direction_input = ucd9000_gpio_direction_input;
4218c2ecf20Sopenharmony_ci	data->gpio.direction_output = ucd9000_gpio_direction_output;
4228c2ecf20Sopenharmony_ci	data->gpio.get = ucd9000_gpio_get;
4238c2ecf20Sopenharmony_ci	data->gpio.set = ucd9000_gpio_set;
4248c2ecf20Sopenharmony_ci	data->gpio.can_sleep = true;
4258c2ecf20Sopenharmony_ci	data->gpio.base = -1;
4268c2ecf20Sopenharmony_ci	data->gpio.parent = &client->dev;
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	rc = devm_gpiochip_add_data(&client->dev, &data->gpio, client);
4298c2ecf20Sopenharmony_ci	if (rc)
4308c2ecf20Sopenharmony_ci		dev_warn(&client->dev, "Could not add gpiochip: %d\n", rc);
4318c2ecf20Sopenharmony_ci}
4328c2ecf20Sopenharmony_ci#else
4338c2ecf20Sopenharmony_cistatic void ucd9000_probe_gpio(struct i2c_client *client,
4348c2ecf20Sopenharmony_ci			       const struct i2c_device_id *mid,
4358c2ecf20Sopenharmony_ci			       struct ucd9000_data *data)
4368c2ecf20Sopenharmony_ci{
4378c2ecf20Sopenharmony_ci}
4388c2ecf20Sopenharmony_ci#endif /* CONFIG_GPIOLIB */
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci#ifdef CONFIG_DEBUG_FS
4418c2ecf20Sopenharmony_cistatic int ucd9000_get_mfr_status(struct i2c_client *client, u8 *buffer)
4428c2ecf20Sopenharmony_ci{
4438c2ecf20Sopenharmony_ci	int ret = pmbus_set_page(client, 0, 0xff);
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci	if (ret < 0)
4468c2ecf20Sopenharmony_ci		return ret;
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	return i2c_smbus_read_block_data(client, UCD9000_MFR_STATUS, buffer);
4498c2ecf20Sopenharmony_ci}
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_cistatic int ucd9000_debugfs_show_mfr_status_bit(void *data, u64 *val)
4528c2ecf20Sopenharmony_ci{
4538c2ecf20Sopenharmony_ci	struct ucd9000_debugfs_entry *entry = data;
4548c2ecf20Sopenharmony_ci	struct i2c_client *client = entry->client;
4558c2ecf20Sopenharmony_ci	u8 buffer[I2C_SMBUS_BLOCK_MAX];
4568c2ecf20Sopenharmony_ci	int ret, i;
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	ret = ucd9000_get_mfr_status(client, buffer);
4598c2ecf20Sopenharmony_ci	if (ret < 0)
4608c2ecf20Sopenharmony_ci		return ret;
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci	/*
4638c2ecf20Sopenharmony_ci	 * GPI fault bits are in sets of 8, two bytes from end of response.
4648c2ecf20Sopenharmony_ci	 */
4658c2ecf20Sopenharmony_ci	i = ret - 3 - entry->index / 8;
4668c2ecf20Sopenharmony_ci	if (i >= 0)
4678c2ecf20Sopenharmony_ci		*val = !!(buffer[i] & BIT(entry->index % 8));
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	return 0;
4708c2ecf20Sopenharmony_ci}
4718c2ecf20Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(ucd9000_debugfs_mfr_status_bit,
4728c2ecf20Sopenharmony_ci			 ucd9000_debugfs_show_mfr_status_bit, NULL, "%1lld\n");
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_cistatic ssize_t ucd9000_debugfs_read_mfr_status(struct file *file,
4758c2ecf20Sopenharmony_ci					       char __user *buf, size_t count,
4768c2ecf20Sopenharmony_ci					       loff_t *ppos)
4778c2ecf20Sopenharmony_ci{
4788c2ecf20Sopenharmony_ci	struct i2c_client *client = file->private_data;
4798c2ecf20Sopenharmony_ci	u8 buffer[I2C_SMBUS_BLOCK_MAX];
4808c2ecf20Sopenharmony_ci	char str[(I2C_SMBUS_BLOCK_MAX * 2) + 2];
4818c2ecf20Sopenharmony_ci	char *res;
4828c2ecf20Sopenharmony_ci	int rc;
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci	rc = ucd9000_get_mfr_status(client, buffer);
4858c2ecf20Sopenharmony_ci	if (rc < 0)
4868c2ecf20Sopenharmony_ci		return rc;
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci	res = bin2hex(str, buffer, min(rc, I2C_SMBUS_BLOCK_MAX));
4898c2ecf20Sopenharmony_ci	*res++ = '\n';
4908c2ecf20Sopenharmony_ci	*res = 0;
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	return simple_read_from_buffer(buf, count, ppos, str, res - str);
4938c2ecf20Sopenharmony_ci}
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_cistatic const struct file_operations ucd9000_debugfs_show_mfr_status_fops = {
4968c2ecf20Sopenharmony_ci	.llseek = noop_llseek,
4978c2ecf20Sopenharmony_ci	.read = ucd9000_debugfs_read_mfr_status,
4988c2ecf20Sopenharmony_ci	.open = simple_open,
4998c2ecf20Sopenharmony_ci};
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_cistatic int ucd9000_init_debugfs(struct i2c_client *client,
5028c2ecf20Sopenharmony_ci				const struct i2c_device_id *mid,
5038c2ecf20Sopenharmony_ci				struct ucd9000_data *data)
5048c2ecf20Sopenharmony_ci{
5058c2ecf20Sopenharmony_ci	struct dentry *debugfs;
5068c2ecf20Sopenharmony_ci	struct ucd9000_debugfs_entry *entries;
5078c2ecf20Sopenharmony_ci	int i, gpi_count;
5088c2ecf20Sopenharmony_ci	char name[UCD9000_DEBUGFS_NAME_LEN];
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci	debugfs = pmbus_get_debugfs_dir(client);
5118c2ecf20Sopenharmony_ci	if (!debugfs)
5128c2ecf20Sopenharmony_ci		return -ENOENT;
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci	data->debugfs = debugfs_create_dir(client->name, debugfs);
5158c2ecf20Sopenharmony_ci	if (!data->debugfs)
5168c2ecf20Sopenharmony_ci		return -ENOENT;
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci	/*
5198c2ecf20Sopenharmony_ci	 * Of the chips this driver supports, only the UCD9090, UCD90160,
5208c2ecf20Sopenharmony_ci	 * UCD90320, and UCD90910 report GPI faults in their MFR_STATUS
5218c2ecf20Sopenharmony_ci	 * register, so only create the GPI fault debugfs attributes for those
5228c2ecf20Sopenharmony_ci	 * chips.
5238c2ecf20Sopenharmony_ci	 */
5248c2ecf20Sopenharmony_ci	if (mid->driver_data == ucd9090 || mid->driver_data == ucd90160 ||
5258c2ecf20Sopenharmony_ci	    mid->driver_data == ucd90320 || mid->driver_data == ucd90910) {
5268c2ecf20Sopenharmony_ci		gpi_count = mid->driver_data == ucd90320 ? UCD90320_GPI_COUNT
5278c2ecf20Sopenharmony_ci							 : UCD9000_GPI_COUNT;
5288c2ecf20Sopenharmony_ci		entries = devm_kcalloc(&client->dev,
5298c2ecf20Sopenharmony_ci				       gpi_count, sizeof(*entries),
5308c2ecf20Sopenharmony_ci				       GFP_KERNEL);
5318c2ecf20Sopenharmony_ci		if (!entries)
5328c2ecf20Sopenharmony_ci			return -ENOMEM;
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci		for (i = 0; i < gpi_count; i++) {
5358c2ecf20Sopenharmony_ci			entries[i].client = client;
5368c2ecf20Sopenharmony_ci			entries[i].index = i;
5378c2ecf20Sopenharmony_ci			scnprintf(name, UCD9000_DEBUGFS_NAME_LEN,
5388c2ecf20Sopenharmony_ci				  "gpi%d_alarm", i + 1);
5398c2ecf20Sopenharmony_ci			debugfs_create_file(name, 0444, data->debugfs,
5408c2ecf20Sopenharmony_ci					    &entries[i],
5418c2ecf20Sopenharmony_ci					    &ucd9000_debugfs_mfr_status_bit);
5428c2ecf20Sopenharmony_ci		}
5438c2ecf20Sopenharmony_ci	}
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci	scnprintf(name, UCD9000_DEBUGFS_NAME_LEN, "mfr_status");
5468c2ecf20Sopenharmony_ci	debugfs_create_file(name, 0444, data->debugfs, client,
5478c2ecf20Sopenharmony_ci			    &ucd9000_debugfs_show_mfr_status_fops);
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci	return 0;
5508c2ecf20Sopenharmony_ci}
5518c2ecf20Sopenharmony_ci#else
5528c2ecf20Sopenharmony_cistatic int ucd9000_init_debugfs(struct i2c_client *client,
5538c2ecf20Sopenharmony_ci				const struct i2c_device_id *mid,
5548c2ecf20Sopenharmony_ci				struct ucd9000_data *data)
5558c2ecf20Sopenharmony_ci{
5568c2ecf20Sopenharmony_ci	return 0;
5578c2ecf20Sopenharmony_ci}
5588c2ecf20Sopenharmony_ci#endif /* CONFIG_DEBUG_FS */
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_cistatic int ucd9000_probe(struct i2c_client *client)
5618c2ecf20Sopenharmony_ci{
5628c2ecf20Sopenharmony_ci	u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
5638c2ecf20Sopenharmony_ci	struct ucd9000_data *data;
5648c2ecf20Sopenharmony_ci	struct pmbus_driver_info *info;
5658c2ecf20Sopenharmony_ci	const struct i2c_device_id *mid;
5668c2ecf20Sopenharmony_ci	enum chips chip;
5678c2ecf20Sopenharmony_ci	int i, ret;
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci	if (!i2c_check_functionality(client->adapter,
5708c2ecf20Sopenharmony_ci				     I2C_FUNC_SMBUS_BYTE_DATA |
5718c2ecf20Sopenharmony_ci				     I2C_FUNC_SMBUS_BLOCK_DATA))
5728c2ecf20Sopenharmony_ci		return -ENODEV;
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci	ret = i2c_smbus_read_block_data(client, UCD9000_DEVICE_ID,
5758c2ecf20Sopenharmony_ci					block_buffer);
5768c2ecf20Sopenharmony_ci	if (ret < 0) {
5778c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Failed to read device ID\n");
5788c2ecf20Sopenharmony_ci		return ret;
5798c2ecf20Sopenharmony_ci	}
5808c2ecf20Sopenharmony_ci	block_buffer[ret] = '\0';
5818c2ecf20Sopenharmony_ci	dev_info(&client->dev, "Device ID %s\n", block_buffer);
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci	for (mid = ucd9000_id; mid->name[0]; mid++) {
5848c2ecf20Sopenharmony_ci		if (!strncasecmp(mid->name, block_buffer, strlen(mid->name)))
5858c2ecf20Sopenharmony_ci			break;
5868c2ecf20Sopenharmony_ci	}
5878c2ecf20Sopenharmony_ci	if (!mid->name[0]) {
5888c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Unsupported device\n");
5898c2ecf20Sopenharmony_ci		return -ENODEV;
5908c2ecf20Sopenharmony_ci	}
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci	if (client->dev.of_node)
5938c2ecf20Sopenharmony_ci		chip = (enum chips)of_device_get_match_data(&client->dev);
5948c2ecf20Sopenharmony_ci	else
5958c2ecf20Sopenharmony_ci		chip = mid->driver_data;
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci	if (chip != ucd9000 && strcmp(client->name, mid->name) != 0)
5988c2ecf20Sopenharmony_ci		dev_notice(&client->dev,
5998c2ecf20Sopenharmony_ci			   "Device mismatch: Configured %s, detected %s\n",
6008c2ecf20Sopenharmony_ci			   client->name, mid->name);
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci	data = devm_kzalloc(&client->dev, sizeof(struct ucd9000_data),
6038c2ecf20Sopenharmony_ci			    GFP_KERNEL);
6048c2ecf20Sopenharmony_ci	if (!data)
6058c2ecf20Sopenharmony_ci		return -ENOMEM;
6068c2ecf20Sopenharmony_ci	info = &data->info;
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci	ret = i2c_smbus_read_byte_data(client, UCD9000_NUM_PAGES);
6098c2ecf20Sopenharmony_ci	if (ret < 0) {
6108c2ecf20Sopenharmony_ci		dev_err(&client->dev,
6118c2ecf20Sopenharmony_ci			"Failed to read number of active pages\n");
6128c2ecf20Sopenharmony_ci		return ret;
6138c2ecf20Sopenharmony_ci	}
6148c2ecf20Sopenharmony_ci	info->pages = ret;
6158c2ecf20Sopenharmony_ci	if (!info->pages) {
6168c2ecf20Sopenharmony_ci		dev_err(&client->dev, "No pages configured\n");
6178c2ecf20Sopenharmony_ci		return -ENODEV;
6188c2ecf20Sopenharmony_ci	}
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci	/* The internal temperature sensor is always active */
6218c2ecf20Sopenharmony_ci	info->func[0] = PMBUS_HAVE_TEMP;
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ci	/* Everything else is configurable */
6248c2ecf20Sopenharmony_ci	ret = i2c_smbus_read_block_data(client, UCD9000_MONITOR_CONFIG,
6258c2ecf20Sopenharmony_ci					block_buffer);
6268c2ecf20Sopenharmony_ci	if (ret <= 0) {
6278c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Failed to read configuration data\n");
6288c2ecf20Sopenharmony_ci		return -ENODEV;
6298c2ecf20Sopenharmony_ci	}
6308c2ecf20Sopenharmony_ci	for (i = 0; i < ret; i++) {
6318c2ecf20Sopenharmony_ci		int page = UCD9000_MON_PAGE(block_buffer[i]);
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci		if (page >= info->pages)
6348c2ecf20Sopenharmony_ci			continue;
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci		switch (UCD9000_MON_TYPE(block_buffer[i])) {
6378c2ecf20Sopenharmony_ci		case UCD9000_MON_VOLTAGE:
6388c2ecf20Sopenharmony_ci		case UCD9000_MON_VOLTAGE_HW:
6398c2ecf20Sopenharmony_ci			info->func[page] |= PMBUS_HAVE_VOUT
6408c2ecf20Sopenharmony_ci			  | PMBUS_HAVE_STATUS_VOUT;
6418c2ecf20Sopenharmony_ci			break;
6428c2ecf20Sopenharmony_ci		case UCD9000_MON_TEMPERATURE:
6438c2ecf20Sopenharmony_ci			info->func[page] |= PMBUS_HAVE_TEMP2
6448c2ecf20Sopenharmony_ci			  | PMBUS_HAVE_STATUS_TEMP;
6458c2ecf20Sopenharmony_ci			break;
6468c2ecf20Sopenharmony_ci		case UCD9000_MON_CURRENT:
6478c2ecf20Sopenharmony_ci			info->func[page] |= PMBUS_HAVE_IOUT
6488c2ecf20Sopenharmony_ci			  | PMBUS_HAVE_STATUS_IOUT;
6498c2ecf20Sopenharmony_ci			break;
6508c2ecf20Sopenharmony_ci		default:
6518c2ecf20Sopenharmony_ci			break;
6528c2ecf20Sopenharmony_ci		}
6538c2ecf20Sopenharmony_ci	}
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci	/* Fan configuration */
6568c2ecf20Sopenharmony_ci	if (mid->driver_data == ucd90124) {
6578c2ecf20Sopenharmony_ci		for (i = 0; i < UCD9000_NUM_FAN; i++) {
6588c2ecf20Sopenharmony_ci			i2c_smbus_write_byte_data(client,
6598c2ecf20Sopenharmony_ci						  UCD9000_FAN_CONFIG_INDEX, i);
6608c2ecf20Sopenharmony_ci			ret = i2c_smbus_read_block_data(client,
6618c2ecf20Sopenharmony_ci							UCD9000_FAN_CONFIG,
6628c2ecf20Sopenharmony_ci							data->fan_data[i]);
6638c2ecf20Sopenharmony_ci			if (ret < 0)
6648c2ecf20Sopenharmony_ci				return ret;
6658c2ecf20Sopenharmony_ci		}
6668c2ecf20Sopenharmony_ci		i2c_smbus_write_byte_data(client, UCD9000_FAN_CONFIG_INDEX, 0);
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci		info->read_byte_data = ucd9000_read_byte_data;
6698c2ecf20Sopenharmony_ci		info->func[0] |= PMBUS_HAVE_FAN12 | PMBUS_HAVE_STATUS_FAN12
6708c2ecf20Sopenharmony_ci		  | PMBUS_HAVE_FAN34 | PMBUS_HAVE_STATUS_FAN34;
6718c2ecf20Sopenharmony_ci	} else if (mid->driver_data == ucd90320) {
6728c2ecf20Sopenharmony_ci		info->read_byte_data = ucd90320_read_byte_data;
6738c2ecf20Sopenharmony_ci		info->read_word_data = ucd90320_read_word_data;
6748c2ecf20Sopenharmony_ci		info->write_byte = ucd90320_write_byte;
6758c2ecf20Sopenharmony_ci		info->write_word_data = ucd90320_write_word_data;
6768c2ecf20Sopenharmony_ci	}
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ci	ucd9000_probe_gpio(client, mid, data);
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_ci	ret = pmbus_do_probe(client, info);
6818c2ecf20Sopenharmony_ci	if (ret)
6828c2ecf20Sopenharmony_ci		return ret;
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci	ret = ucd9000_init_debugfs(client, mid, data);
6858c2ecf20Sopenharmony_ci	if (ret)
6868c2ecf20Sopenharmony_ci		dev_warn(&client->dev, "Failed to register debugfs: %d\n",
6878c2ecf20Sopenharmony_ci			 ret);
6888c2ecf20Sopenharmony_ci
6898c2ecf20Sopenharmony_ci	return 0;
6908c2ecf20Sopenharmony_ci}
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_ci/* This is the driver that will be inserted */
6938c2ecf20Sopenharmony_cistatic struct i2c_driver ucd9000_driver = {
6948c2ecf20Sopenharmony_ci	.driver = {
6958c2ecf20Sopenharmony_ci		.name = "ucd9000",
6968c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(ucd9000_of_match),
6978c2ecf20Sopenharmony_ci	},
6988c2ecf20Sopenharmony_ci	.probe_new = ucd9000_probe,
6998c2ecf20Sopenharmony_ci	.remove = pmbus_do_remove,
7008c2ecf20Sopenharmony_ci	.id_table = ucd9000_id,
7018c2ecf20Sopenharmony_ci};
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_cimodule_i2c_driver(ucd9000_driver);
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ciMODULE_AUTHOR("Guenter Roeck");
7068c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("PMBus driver for TI UCD90xxx");
7078c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
708