xref: /openbmc/linux/drivers/pinctrl/pinctrl-sx150x.c (revision 6489677f86c330404ae47703bf59d30ec4c46de9)
19e80f906SNeil Armstrong /*
29e80f906SNeil Armstrong  * Copyright (c) 2016, BayLibre, SAS. All rights reserved.
39e80f906SNeil Armstrong  * Author: Neil Armstrong <narmstrong@baylibre.com>
49e80f906SNeil Armstrong  *
59e80f906SNeil Armstrong  * Copyright (c) 2010, Code Aurora Forum. All rights reserved.
69e80f906SNeil Armstrong  *
79e80f906SNeil Armstrong  * Driver for Semtech SX150X I2C GPIO Expanders
89e80f906SNeil Armstrong  *
99e80f906SNeil Armstrong  * Author: Gregory Bean <gbean@codeaurora.org>
109e80f906SNeil Armstrong  *
119e80f906SNeil Armstrong  * This program is free software; you can redistribute it and/or modify
129e80f906SNeil Armstrong  * it under the terms of the GNU General Public License version 2 and
139e80f906SNeil Armstrong  * only version 2 as published by the Free Software Foundation.
149e80f906SNeil Armstrong  *
159e80f906SNeil Armstrong  * This program is distributed in the hope that it will be useful,
169e80f906SNeil Armstrong  * but WITHOUT ANY WARRANTY; without even the implied warranty of
179e80f906SNeil Armstrong  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
189e80f906SNeil Armstrong  * GNU General Public License for more details.
199e80f906SNeil Armstrong  */
209e80f906SNeil Armstrong 
210db0f26cSAndrey Smirnov #include <linux/regmap.h>
229e80f906SNeil Armstrong #include <linux/i2c.h>
239e80f906SNeil Armstrong #include <linux/init.h>
249e80f906SNeil Armstrong #include <linux/interrupt.h>
259e80f906SNeil Armstrong #include <linux/irq.h>
269e80f906SNeil Armstrong #include <linux/mutex.h>
279e80f906SNeil Armstrong #include <linux/slab.h>
289e80f906SNeil Armstrong #include <linux/of.h>
29e3ba8120SAndrey Smirnov #include <linux/of_device.h>
309e80f906SNeil Armstrong #include <linux/gpio.h>
319e80f906SNeil Armstrong #include <linux/pinctrl/machine.h>
329e80f906SNeil Armstrong #include <linux/pinctrl/pinconf.h>
339e80f906SNeil Armstrong #include <linux/pinctrl/pinctrl.h>
349e80f906SNeil Armstrong #include <linux/pinctrl/pinmux.h>
359e80f906SNeil Armstrong #include <linux/pinctrl/pinconf-generic.h>
369e80f906SNeil Armstrong 
379e80f906SNeil Armstrong #include "core.h"
389e80f906SNeil Armstrong #include "pinconf.h"
399e80f906SNeil Armstrong #include "pinctrl-utils.h"
409e80f906SNeil Armstrong 
419e80f906SNeil Armstrong /* The chip models of sx150x */
429e80f906SNeil Armstrong enum {
439e80f906SNeil Armstrong 	SX150X_123 = 0,
449e80f906SNeil Armstrong 	SX150X_456,
459e80f906SNeil Armstrong 	SX150X_789,
469e80f906SNeil Armstrong };
477d68a79aSAndrey Smirnov enum {
487d68a79aSAndrey Smirnov 	SX150X_789_REG_MISC_AUTOCLEAR_OFF = 1 << 0,
490db0f26cSAndrey Smirnov 	SX150X_MAX_REGISTER = 0xad,
507d68a79aSAndrey Smirnov };
519e80f906SNeil Armstrong 
529e80f906SNeil Armstrong struct sx150x_123_pri {
539e80f906SNeil Armstrong 	u8 reg_pld_mode;
549e80f906SNeil Armstrong 	u8 reg_pld_table0;
559e80f906SNeil Armstrong 	u8 reg_pld_table1;
569e80f906SNeil Armstrong 	u8 reg_pld_table2;
579e80f906SNeil Armstrong 	u8 reg_pld_table3;
589e80f906SNeil Armstrong 	u8 reg_pld_table4;
599e80f906SNeil Armstrong 	u8 reg_advance;
609e80f906SNeil Armstrong };
619e80f906SNeil Armstrong 
629e80f906SNeil Armstrong struct sx150x_456_pri {
639e80f906SNeil Armstrong 	u8 reg_pld_mode;
649e80f906SNeil Armstrong 	u8 reg_pld_table0;
659e80f906SNeil Armstrong 	u8 reg_pld_table1;
669e80f906SNeil Armstrong 	u8 reg_pld_table2;
679e80f906SNeil Armstrong 	u8 reg_pld_table3;
689e80f906SNeil Armstrong 	u8 reg_pld_table4;
699e80f906SNeil Armstrong 	u8 reg_advance;
709e80f906SNeil Armstrong };
719e80f906SNeil Armstrong 
729e80f906SNeil Armstrong struct sx150x_789_pri {
739e80f906SNeil Armstrong 	u8 reg_drain;
749e80f906SNeil Armstrong 	u8 reg_polarity;
759e80f906SNeil Armstrong 	u8 reg_clock;
769e80f906SNeil Armstrong 	u8 reg_misc;
779e80f906SNeil Armstrong 	u8 reg_reset;
789e80f906SNeil Armstrong 	u8 ngpios;
799e80f906SNeil Armstrong };
809e80f906SNeil Armstrong 
819e80f906SNeil Armstrong struct sx150x_device_data {
829e80f906SNeil Armstrong 	u8 model;
839e80f906SNeil Armstrong 	u8 reg_pullup;
849e80f906SNeil Armstrong 	u8 reg_pulldn;
859e80f906SNeil Armstrong 	u8 reg_dir;
869e80f906SNeil Armstrong 	u8 reg_data;
879e80f906SNeil Armstrong 	u8 reg_irq_mask;
889e80f906SNeil Armstrong 	u8 reg_irq_src;
899e80f906SNeil Armstrong 	u8 reg_sense;
909e80f906SNeil Armstrong 	u8 ngpios;
919e80f906SNeil Armstrong 	union {
929e80f906SNeil Armstrong 		struct sx150x_123_pri x123;
939e80f906SNeil Armstrong 		struct sx150x_456_pri x456;
949e80f906SNeil Armstrong 		struct sx150x_789_pri x789;
959e80f906SNeil Armstrong 	} pri;
969e80f906SNeil Armstrong 	const struct pinctrl_pin_desc *pins;
979e80f906SNeil Armstrong 	unsigned int npins;
989e80f906SNeil Armstrong };
999e80f906SNeil Armstrong 
1009e80f906SNeil Armstrong struct sx150x_pinctrl {
1019e80f906SNeil Armstrong 	struct device *dev;
1029e80f906SNeil Armstrong 	struct i2c_client *client;
1039e80f906SNeil Armstrong 	struct pinctrl_dev *pctldev;
1049e80f906SNeil Armstrong 	struct pinctrl_desc pinctrl_desc;
1059e80f906SNeil Armstrong 	struct gpio_chip gpio;
1069e80f906SNeil Armstrong 	struct irq_chip irq_chip;
1070db0f26cSAndrey Smirnov 	struct regmap *regmap;
1089e80f906SNeil Armstrong 	struct {
1099e80f906SNeil Armstrong 		u32 sense;
1109e80f906SNeil Armstrong 		u32 masked;
1119e80f906SNeil Armstrong 	} irq;
1129e80f906SNeil Armstrong 	struct mutex lock;
1139e80f906SNeil Armstrong 	const struct sx150x_device_data *data;
1149e80f906SNeil Armstrong };
1159e80f906SNeil Armstrong 
1169e80f906SNeil Armstrong static const struct pinctrl_pin_desc sx150x_8_pins[] = {
1179e80f906SNeil Armstrong 	PINCTRL_PIN(0, "gpio0"),
1189e80f906SNeil Armstrong 	PINCTRL_PIN(1, "gpio1"),
1199e80f906SNeil Armstrong 	PINCTRL_PIN(2, "gpio2"),
1209e80f906SNeil Armstrong 	PINCTRL_PIN(3, "gpio3"),
1219e80f906SNeil Armstrong 	PINCTRL_PIN(4, "gpio4"),
1229e80f906SNeil Armstrong 	PINCTRL_PIN(5, "gpio5"),
1239e80f906SNeil Armstrong 	PINCTRL_PIN(6, "gpio6"),
1249e80f906SNeil Armstrong 	PINCTRL_PIN(7, "gpio7"),
1259e80f906SNeil Armstrong 	PINCTRL_PIN(8, "oscio"),
1269e80f906SNeil Armstrong };
1279e80f906SNeil Armstrong 
1289e80f906SNeil Armstrong static const struct pinctrl_pin_desc sx150x_16_pins[] = {
1299e80f906SNeil Armstrong 	PINCTRL_PIN(0, "gpio0"),
1309e80f906SNeil Armstrong 	PINCTRL_PIN(1, "gpio1"),
1319e80f906SNeil Armstrong 	PINCTRL_PIN(2, "gpio2"),
1329e80f906SNeil Armstrong 	PINCTRL_PIN(3, "gpio3"),
1339e80f906SNeil Armstrong 	PINCTRL_PIN(4, "gpio4"),
1349e80f906SNeil Armstrong 	PINCTRL_PIN(5, "gpio5"),
1359e80f906SNeil Armstrong 	PINCTRL_PIN(6, "gpio6"),
1369e80f906SNeil Armstrong 	PINCTRL_PIN(7, "gpio7"),
1379e80f906SNeil Armstrong 	PINCTRL_PIN(8, "gpio8"),
1389e80f906SNeil Armstrong 	PINCTRL_PIN(9, "gpio9"),
1399e80f906SNeil Armstrong 	PINCTRL_PIN(10, "gpio10"),
1409e80f906SNeil Armstrong 	PINCTRL_PIN(11, "gpio11"),
1419e80f906SNeil Armstrong 	PINCTRL_PIN(12, "gpio12"),
1429e80f906SNeil Armstrong 	PINCTRL_PIN(13, "gpio13"),
1439e80f906SNeil Armstrong 	PINCTRL_PIN(14, "gpio14"),
1449e80f906SNeil Armstrong 	PINCTRL_PIN(15, "gpio15"),
1459e80f906SNeil Armstrong 	PINCTRL_PIN(16, "oscio"),
1469e80f906SNeil Armstrong };
1479e80f906SNeil Armstrong 
1489e80f906SNeil Armstrong static const struct sx150x_device_data sx1508q_device_data = {
1499e80f906SNeil Armstrong 	.model = SX150X_789,
1509e80f906SNeil Armstrong 	.reg_pullup	= 0x03,
1519e80f906SNeil Armstrong 	.reg_pulldn	= 0x04,
1529e80f906SNeil Armstrong 	.reg_dir	= 0x07,
1539e80f906SNeil Armstrong 	.reg_data	= 0x08,
1549e80f906SNeil Armstrong 	.reg_irq_mask	= 0x09,
1559e80f906SNeil Armstrong 	.reg_irq_src	= 0x0c,
1569e80f906SNeil Armstrong 	.reg_sense	= 0x0b,
1579e80f906SNeil Armstrong 	.pri.x789 = {
1589e80f906SNeil Armstrong 		.reg_drain	= 0x05,
1599e80f906SNeil Armstrong 		.reg_polarity	= 0x06,
1609e80f906SNeil Armstrong 		.reg_clock	= 0x0f,
1619e80f906SNeil Armstrong 		.reg_misc	= 0x10,
1629e80f906SNeil Armstrong 		.reg_reset	= 0x7d,
1639e80f906SNeil Armstrong 	},
1649e80f906SNeil Armstrong 	.ngpios = 8,
1659e80f906SNeil Armstrong 	.pins = sx150x_8_pins,
1669e80f906SNeil Armstrong 	.npins = ARRAY_SIZE(sx150x_8_pins),
1679e80f906SNeil Armstrong };
1689e80f906SNeil Armstrong 
1699e80f906SNeil Armstrong static const struct sx150x_device_data sx1509q_device_data = {
1709e80f906SNeil Armstrong 	.model = SX150X_789,
171*6489677fSAndrey Smirnov 	.reg_pullup	= 0x06,
172*6489677fSAndrey Smirnov 	.reg_pulldn	= 0x08,
173*6489677fSAndrey Smirnov 	.reg_dir	= 0x0e,
174*6489677fSAndrey Smirnov 	.reg_data	= 0x10,
175*6489677fSAndrey Smirnov 	.reg_irq_mask	= 0x12,
176*6489677fSAndrey Smirnov 	.reg_irq_src	= 0x18,
177*6489677fSAndrey Smirnov 	.reg_sense	= 0x14,
1789e80f906SNeil Armstrong 	.pri.x789 = {
179*6489677fSAndrey Smirnov 		.reg_drain	= 0x0a,
180*6489677fSAndrey Smirnov 		.reg_polarity	= 0x0c,
1819e80f906SNeil Armstrong 		.reg_clock	= 0x1e,
1829e80f906SNeil Armstrong 		.reg_misc	= 0x1f,
1839e80f906SNeil Armstrong 		.reg_reset	= 0x7d,
1849e80f906SNeil Armstrong 	},
1859e80f906SNeil Armstrong 	.ngpios	= 16,
1869e80f906SNeil Armstrong 	.pins = sx150x_16_pins,
1879e80f906SNeil Armstrong 	.npins = ARRAY_SIZE(sx150x_16_pins),
1889e80f906SNeil Armstrong };
1899e80f906SNeil Armstrong 
1909e80f906SNeil Armstrong static const struct sx150x_device_data sx1506q_device_data = {
1919e80f906SNeil Armstrong 	.model = SX150X_456,
192*6489677fSAndrey Smirnov 	.reg_pullup	= 0x04,
193*6489677fSAndrey Smirnov 	.reg_pulldn	= 0x06,
194*6489677fSAndrey Smirnov 	.reg_dir	= 0x02,
195*6489677fSAndrey Smirnov 	.reg_data	= 0x00,
196*6489677fSAndrey Smirnov 	.reg_irq_mask	= 0x08,
197*6489677fSAndrey Smirnov 	.reg_irq_src	= 0x0e,
198*6489677fSAndrey Smirnov 	.reg_sense	= 0x0a,
1999e80f906SNeil Armstrong 	.pri.x456 = {
200*6489677fSAndrey Smirnov 		.reg_pld_mode	= 0x20,
201*6489677fSAndrey Smirnov 		.reg_pld_table0	= 0x22,
202*6489677fSAndrey Smirnov 		.reg_pld_table1	= 0x24,
203*6489677fSAndrey Smirnov 		.reg_pld_table2	= 0x26,
204*6489677fSAndrey Smirnov 		.reg_pld_table3	= 0x28,
205*6489677fSAndrey Smirnov 		.reg_pld_table4	= 0x2a,
2069e80f906SNeil Armstrong 		.reg_advance	= 0xad,
2079e80f906SNeil Armstrong 	},
2089e80f906SNeil Armstrong 	.ngpios	= 16,
2099e80f906SNeil Armstrong 	.pins = sx150x_16_pins,
2109e80f906SNeil Armstrong 	.npins = 16, /* oscio not available */
2119e80f906SNeil Armstrong };
2129e80f906SNeil Armstrong 
2139e80f906SNeil Armstrong static const struct sx150x_device_data sx1502q_device_data = {
2149e80f906SNeil Armstrong 	.model = SX150X_123,
2159e80f906SNeil Armstrong 	.reg_pullup	= 0x02,
2169e80f906SNeil Armstrong 	.reg_pulldn	= 0x03,
2179e80f906SNeil Armstrong 	.reg_dir	= 0x01,
2189e80f906SNeil Armstrong 	.reg_data	= 0x00,
2199e80f906SNeil Armstrong 	.reg_irq_mask	= 0x05,
2209e80f906SNeil Armstrong 	.reg_irq_src	= 0x08,
2219e80f906SNeil Armstrong 	.reg_sense	= 0x07,
2229e80f906SNeil Armstrong 	.pri.x123 = {
2239e80f906SNeil Armstrong 		.reg_pld_mode	= 0x10,
2249e80f906SNeil Armstrong 		.reg_pld_table0	= 0x11,
2259e80f906SNeil Armstrong 		.reg_pld_table1	= 0x12,
2269e80f906SNeil Armstrong 		.reg_pld_table2	= 0x13,
2279e80f906SNeil Armstrong 		.reg_pld_table3	= 0x14,
2289e80f906SNeil Armstrong 		.reg_pld_table4	= 0x15,
2299e80f906SNeil Armstrong 		.reg_advance	= 0xad,
2309e80f906SNeil Armstrong 	},
2319e80f906SNeil Armstrong 	.ngpios	= 8,
2329e80f906SNeil Armstrong 	.pins = sx150x_8_pins,
2339e80f906SNeil Armstrong 	.npins = 8, /* oscio not available */
2349e80f906SNeil Armstrong };
2359e80f906SNeil Armstrong 
2366697546dSAndrey Smirnov static const struct sx150x_device_data sx1503q_device_data = {
2376697546dSAndrey Smirnov 	.model = SX150X_123,
238*6489677fSAndrey Smirnov 	.reg_pullup	= 0x04,
239*6489677fSAndrey Smirnov 	.reg_pulldn	= 0x06,
240*6489677fSAndrey Smirnov 	.reg_dir	= 0x02,
241*6489677fSAndrey Smirnov 	.reg_data	= 0x00,
242*6489677fSAndrey Smirnov 	.reg_irq_mask	= 0x08,
243*6489677fSAndrey Smirnov 	.reg_irq_src	= 0x0e,
244*6489677fSAndrey Smirnov 	.reg_sense	= 0x0a,
2456697546dSAndrey Smirnov 	.pri.x123 = {
246*6489677fSAndrey Smirnov 		.reg_pld_mode	= 0x20,
247*6489677fSAndrey Smirnov 		.reg_pld_table0	= 0x22,
248*6489677fSAndrey Smirnov 		.reg_pld_table1	= 0x24,
249*6489677fSAndrey Smirnov 		.reg_pld_table2	= 0x26,
250*6489677fSAndrey Smirnov 		.reg_pld_table3	= 0x28,
251*6489677fSAndrey Smirnov 		.reg_pld_table4	= 0x2a,
2526697546dSAndrey Smirnov 		.reg_advance	= 0xad,
2536697546dSAndrey Smirnov 	},
2546697546dSAndrey Smirnov 	.ngpios	= 16,
2556697546dSAndrey Smirnov 	.pins = sx150x_16_pins,
2566697546dSAndrey Smirnov 	.npins  = 16, /* oscio not available */
2576697546dSAndrey Smirnov };
2586697546dSAndrey Smirnov 
2599e80f906SNeil Armstrong static int sx150x_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
2609e80f906SNeil Armstrong {
2619e80f906SNeil Armstrong 	return 0;
2629e80f906SNeil Armstrong }
2639e80f906SNeil Armstrong 
2649e80f906SNeil Armstrong static const char *sx150x_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
2659e80f906SNeil Armstrong 						unsigned int group)
2669e80f906SNeil Armstrong {
2679e80f906SNeil Armstrong 	return NULL;
2689e80f906SNeil Armstrong }
2699e80f906SNeil Armstrong 
2709e80f906SNeil Armstrong static int sx150x_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
2719e80f906SNeil Armstrong 					unsigned int group,
2729e80f906SNeil Armstrong 					const unsigned int **pins,
2739e80f906SNeil Armstrong 					unsigned int *num_pins)
2749e80f906SNeil Armstrong {
2759e80f906SNeil Armstrong 	return -ENOTSUPP;
2769e80f906SNeil Armstrong }
2779e80f906SNeil Armstrong 
2789e80f906SNeil Armstrong static const struct pinctrl_ops sx150x_pinctrl_ops = {
2799e80f906SNeil Armstrong 	.get_groups_count = sx150x_pinctrl_get_groups_count,
2809e80f906SNeil Armstrong 	.get_group_name = sx150x_pinctrl_get_group_name,
2819e80f906SNeil Armstrong 	.get_group_pins = sx150x_pinctrl_get_group_pins,
2829e80f906SNeil Armstrong #ifdef CONFIG_OF
2839e80f906SNeil Armstrong 	.dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
2849e80f906SNeil Armstrong 	.dt_free_map = pinctrl_utils_free_map,
2859e80f906SNeil Armstrong #endif
2869e80f906SNeil Armstrong };
2879e80f906SNeil Armstrong 
2889e80f906SNeil Armstrong static bool sx150x_pin_is_oscio(struct sx150x_pinctrl *pctl, unsigned int pin)
2899e80f906SNeil Armstrong {
2909e80f906SNeil Armstrong 	if (pin >= pctl->data->npins)
2919e80f906SNeil Armstrong 		return false;
2929e80f906SNeil Armstrong 
2939e80f906SNeil Armstrong 	/* OSCIO pin is only present in 789 devices */
2949e80f906SNeil Armstrong 	if (pctl->data->model != SX150X_789)
2959e80f906SNeil Armstrong 		return false;
2969e80f906SNeil Armstrong 
2979e80f906SNeil Armstrong 	return !strcmp(pctl->data->pins[pin].name, "oscio");
2989e80f906SNeil Armstrong }
2999e80f906SNeil Armstrong 
3009e80f906SNeil Armstrong static int sx150x_gpio_get_direction(struct gpio_chip *chip,
3019e80f906SNeil Armstrong 				      unsigned int offset)
3029e80f906SNeil Armstrong {
3039e80f906SNeil Armstrong 	struct sx150x_pinctrl *pctl = gpiochip_get_data(chip);
304*6489677fSAndrey Smirnov 	unsigned int value;
305*6489677fSAndrey Smirnov 	int ret;
3069e80f906SNeil Armstrong 
3079e80f906SNeil Armstrong 	if (sx150x_pin_is_oscio(pctl, offset))
3089e80f906SNeil Armstrong 		return false;
3099e80f906SNeil Armstrong 
310*6489677fSAndrey Smirnov 	ret = regmap_read(pctl->regmap, pctl->data->reg_dir, &value);
311*6489677fSAndrey Smirnov 	if (ret < 0)
312*6489677fSAndrey Smirnov 		return ret;
3139e80f906SNeil Armstrong 
314*6489677fSAndrey Smirnov 	return !!(value & BIT(offset));
3159e80f906SNeil Armstrong }
3169e80f906SNeil Armstrong 
3179e80f906SNeil Armstrong static int sx150x_gpio_get(struct gpio_chip *chip, unsigned int offset)
3189e80f906SNeil Armstrong {
3199e80f906SNeil Armstrong 	struct sx150x_pinctrl *pctl = gpiochip_get_data(chip);
320*6489677fSAndrey Smirnov 	unsigned int value;
321*6489677fSAndrey Smirnov 	int ret;
3229e80f906SNeil Armstrong 
3239e80f906SNeil Armstrong 	if (sx150x_pin_is_oscio(pctl, offset))
3249e80f906SNeil Armstrong 		return -EINVAL;
3259e80f906SNeil Armstrong 
326*6489677fSAndrey Smirnov 	ret = regmap_read(pctl->regmap, pctl->data->reg_data, &value);
327*6489677fSAndrey Smirnov 	if (ret < 0)
328*6489677fSAndrey Smirnov 		return ret;
3299e80f906SNeil Armstrong 
330*6489677fSAndrey Smirnov 	return !!(value & BIT(offset));
3319e80f906SNeil Armstrong }
3329e80f906SNeil Armstrong 
3339e80f906SNeil Armstrong static int sx150x_gpio_set_single_ended(struct gpio_chip *chip,
3349e80f906SNeil Armstrong 					unsigned int offset,
3359e80f906SNeil Armstrong 					enum single_ended_mode mode)
3369e80f906SNeil Armstrong {
3379e80f906SNeil Armstrong 	struct sx150x_pinctrl *pctl = gpiochip_get_data(chip);
3389e80f906SNeil Armstrong 	int ret;
3399e80f906SNeil Armstrong 
3409e80f906SNeil Armstrong 	switch (mode) {
3419e80f906SNeil Armstrong 	case LINE_MODE_PUSH_PULL:
3429e80f906SNeil Armstrong 		if (pctl->data->model != SX150X_789 ||
3439e80f906SNeil Armstrong 		    sx150x_pin_is_oscio(pctl, offset))
3449e80f906SNeil Armstrong 			return 0;
3459e80f906SNeil Armstrong 
3469e80f906SNeil Armstrong 		mutex_lock(&pctl->lock);
347*6489677fSAndrey Smirnov 		ret = regmap_write_bits(pctl->regmap,
3489e80f906SNeil Armstrong 					pctl->data->pri.x789.reg_drain,
349*6489677fSAndrey Smirnov 					BIT(offset), 0);
3509e80f906SNeil Armstrong 		mutex_unlock(&pctl->lock);
3519e80f906SNeil Armstrong 		if (ret < 0)
3529e80f906SNeil Armstrong 			return ret;
3539e80f906SNeil Armstrong 		break;
3549e80f906SNeil Armstrong 
3559e80f906SNeil Armstrong 	case LINE_MODE_OPEN_DRAIN:
3569e80f906SNeil Armstrong 		if (pctl->data->model != SX150X_789 ||
3579e80f906SNeil Armstrong 		    sx150x_pin_is_oscio(pctl, offset))
3589e80f906SNeil Armstrong 			return -ENOTSUPP;
3599e80f906SNeil Armstrong 
3609e80f906SNeil Armstrong 		mutex_lock(&pctl->lock);
361*6489677fSAndrey Smirnov 		ret = regmap_write_bits(pctl->regmap,
3629e80f906SNeil Armstrong 					pctl->data->pri.x789.reg_drain,
363*6489677fSAndrey Smirnov 					BIT(offset), BIT(offset));
3649e80f906SNeil Armstrong 		mutex_unlock(&pctl->lock);
3659e80f906SNeil Armstrong 		if (ret < 0)
3669e80f906SNeil Armstrong 			return ret;
3679e80f906SNeil Armstrong 		break;
3689e80f906SNeil Armstrong 
3699e80f906SNeil Armstrong 	default:
3709e80f906SNeil Armstrong 		return -ENOTSUPP;
3719e80f906SNeil Armstrong 	}
3729e80f906SNeil Armstrong 
3739e80f906SNeil Armstrong 	return 0;
3749e80f906SNeil Armstrong }
3759e80f906SNeil Armstrong 
376*6489677fSAndrey Smirnov static int __sx150x_gpio_set(struct sx150x_pinctrl *pctl, unsigned int offset,
377*6489677fSAndrey Smirnov 			     int value)
378*6489677fSAndrey Smirnov {
379*6489677fSAndrey Smirnov 	return regmap_write_bits(pctl->regmap, pctl->data->reg_data,
380*6489677fSAndrey Smirnov 				 BIT(offset), value ? BIT(offset) : 0);
381*6489677fSAndrey Smirnov }
382*6489677fSAndrey Smirnov 
3839e80f906SNeil Armstrong static void sx150x_gpio_set(struct gpio_chip *chip, unsigned int offset,
3849e80f906SNeil Armstrong 			       int value)
3859e80f906SNeil Armstrong {
3869e80f906SNeil Armstrong 	struct sx150x_pinctrl *pctl = gpiochip_get_data(chip);
3879e80f906SNeil Armstrong 
3889e80f906SNeil Armstrong 	if (sx150x_pin_is_oscio(pctl, offset)) {
3899e80f906SNeil Armstrong 		mutex_lock(&pctl->lock);
3900db0f26cSAndrey Smirnov 		regmap_write(pctl->regmap,
3919e80f906SNeil Armstrong 			     pctl->data->pri.x789.reg_clock,
3929e80f906SNeil Armstrong 			     (value ? 0x1f : 0x10));
3939e80f906SNeil Armstrong 		mutex_unlock(&pctl->lock);
3949e80f906SNeil Armstrong 	} else {
3959e80f906SNeil Armstrong 		mutex_lock(&pctl->lock);
396*6489677fSAndrey Smirnov 		__sx150x_gpio_set(pctl, offset, value);
3979e80f906SNeil Armstrong 		mutex_unlock(&pctl->lock);
3989e80f906SNeil Armstrong 	}
3999e80f906SNeil Armstrong }
4009e80f906SNeil Armstrong 
4019e80f906SNeil Armstrong static int sx150x_gpio_direction_input(struct gpio_chip *chip,
4029e80f906SNeil Armstrong 				      unsigned int offset)
4039e80f906SNeil Armstrong {
4049e80f906SNeil Armstrong 	struct sx150x_pinctrl *pctl = gpiochip_get_data(chip);
4059e80f906SNeil Armstrong 	int ret;
4069e80f906SNeil Armstrong 
4079e80f906SNeil Armstrong 	if (sx150x_pin_is_oscio(pctl, offset))
4089e80f906SNeil Armstrong 		return -EINVAL;
4099e80f906SNeil Armstrong 
4109e80f906SNeil Armstrong 	mutex_lock(&pctl->lock);
411*6489677fSAndrey Smirnov 	ret = regmap_write_bits(pctl->regmap,
412*6489677fSAndrey Smirnov 				pctl->data->reg_dir,
413*6489677fSAndrey Smirnov 				BIT(offset), BIT(offset));
4149e80f906SNeil Armstrong 	mutex_unlock(&pctl->lock);
4159e80f906SNeil Armstrong 
4169e80f906SNeil Armstrong 	return ret;
4179e80f906SNeil Armstrong }
4189e80f906SNeil Armstrong 
4199e80f906SNeil Armstrong static int sx150x_gpio_direction_output(struct gpio_chip *chip,
4209e80f906SNeil Armstrong 				       unsigned int offset, int value)
4219e80f906SNeil Armstrong {
4229e80f906SNeil Armstrong 	struct sx150x_pinctrl *pctl = gpiochip_get_data(chip);
4239e80f906SNeil Armstrong 	int status;
4249e80f906SNeil Armstrong 
4259e80f906SNeil Armstrong 	if (sx150x_pin_is_oscio(pctl, offset)) {
4269e80f906SNeil Armstrong 		sx150x_gpio_set(chip, offset, value);
4279e80f906SNeil Armstrong 		return 0;
4289e80f906SNeil Armstrong 	}
4299e80f906SNeil Armstrong 
4309e80f906SNeil Armstrong 	mutex_lock(&pctl->lock);
431*6489677fSAndrey Smirnov 	status = __sx150x_gpio_set(pctl, offset, value);
4329e80f906SNeil Armstrong 	if (status >= 0)
433*6489677fSAndrey Smirnov 		status = regmap_write_bits(pctl->regmap,
434*6489677fSAndrey Smirnov 					   pctl->data->reg_dir,
435*6489677fSAndrey Smirnov 					   BIT(offset), 0);
4369e80f906SNeil Armstrong 	mutex_unlock(&pctl->lock);
4379e80f906SNeil Armstrong 
4389e80f906SNeil Armstrong 	return status;
4399e80f906SNeil Armstrong }
4409e80f906SNeil Armstrong 
4419e80f906SNeil Armstrong static void sx150x_irq_mask(struct irq_data *d)
4429e80f906SNeil Armstrong {
4439e80f906SNeil Armstrong 	struct sx150x_pinctrl *pctl =
4449e80f906SNeil Armstrong 			gpiochip_get_data(irq_data_get_irq_chip_data(d));
4459e80f906SNeil Armstrong 	unsigned int n = d->hwirq;
4469e80f906SNeil Armstrong 
447*6489677fSAndrey Smirnov 	pctl->irq.masked |= BIT(n);
4489e80f906SNeil Armstrong }
4499e80f906SNeil Armstrong 
4509e80f906SNeil Armstrong static void sx150x_irq_unmask(struct irq_data *d)
4519e80f906SNeil Armstrong {
4529e80f906SNeil Armstrong 	struct sx150x_pinctrl *pctl =
4539e80f906SNeil Armstrong 			gpiochip_get_data(irq_data_get_irq_chip_data(d));
4549e80f906SNeil Armstrong 	unsigned int n = d->hwirq;
4559e80f906SNeil Armstrong 
456*6489677fSAndrey Smirnov 	pctl->irq.masked &= ~BIT(n);
4579e80f906SNeil Armstrong }
4589e80f906SNeil Armstrong 
4599e80f906SNeil Armstrong static int sx150x_irq_set_type(struct irq_data *d, unsigned int flow_type)
4609e80f906SNeil Armstrong {
4619e80f906SNeil Armstrong 	struct sx150x_pinctrl *pctl =
4629e80f906SNeil Armstrong 			gpiochip_get_data(irq_data_get_irq_chip_data(d));
4639e80f906SNeil Armstrong 	unsigned int n, val = 0;
4649e80f906SNeil Armstrong 
4659e80f906SNeil Armstrong 	if (flow_type & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW))
4669e80f906SNeil Armstrong 		return -EINVAL;
4679e80f906SNeil Armstrong 
4689e80f906SNeil Armstrong 	n = d->hwirq;
4699e80f906SNeil Armstrong 
4709e80f906SNeil Armstrong 	if (flow_type & IRQ_TYPE_EDGE_RISING)
4719e80f906SNeil Armstrong 		val |= 0x1;
4729e80f906SNeil Armstrong 	if (flow_type & IRQ_TYPE_EDGE_FALLING)
4739e80f906SNeil Armstrong 		val |= 0x2;
4749e80f906SNeil Armstrong 
4759e80f906SNeil Armstrong 	pctl->irq.sense &= ~(3UL << (n * 2));
4769e80f906SNeil Armstrong 	pctl->irq.sense |= val << (n * 2);
4779e80f906SNeil Armstrong 	return 0;
4789e80f906SNeil Armstrong }
4799e80f906SNeil Armstrong 
4809e80f906SNeil Armstrong static irqreturn_t sx150x_irq_thread_fn(int irq, void *dev_id)
4819e80f906SNeil Armstrong {
4829e80f906SNeil Armstrong 	struct sx150x_pinctrl *pctl = (struct sx150x_pinctrl *)dev_id;
4839e80f906SNeil Armstrong 	unsigned int nhandled = 0;
4849e80f906SNeil Armstrong 	unsigned int sub_irq;
4859e80f906SNeil Armstrong 	unsigned int n;
4869e80f906SNeil Armstrong 	s32 err;
4870db0f26cSAndrey Smirnov 	unsigned int val;
4889e80f906SNeil Armstrong 
489*6489677fSAndrey Smirnov 	err = regmap_read(pctl->regmap, pctl->data->reg_irq_src, &val);
4909e80f906SNeil Armstrong 	if (err < 0)
491*6489677fSAndrey Smirnov 		return IRQ_NONE;
4929e80f906SNeil Armstrong 
493*6489677fSAndrey Smirnov 	err = regmap_write(pctl->regmap, pctl->data->reg_irq_src, val);
4949e80f906SNeil Armstrong 	if (err < 0)
495*6489677fSAndrey Smirnov 		return IRQ_NONE;
4969e80f906SNeil Armstrong 
497*6489677fSAndrey Smirnov 	for (n = 0; n < pctl->data->ngpios; ++n) {
498*6489677fSAndrey Smirnov 		if (val & BIT(n)) {
499*6489677fSAndrey Smirnov 			sub_irq = irq_find_mapping(pctl->gpio.irqdomain, n);
5009e80f906SNeil Armstrong 			handle_nested_irq(sub_irq);
5019e80f906SNeil Armstrong 			++nhandled;
5029e80f906SNeil Armstrong 		}
5039e80f906SNeil Armstrong 	}
5049e80f906SNeil Armstrong 
5059e80f906SNeil Armstrong 	return (nhandled > 0 ? IRQ_HANDLED : IRQ_NONE);
5069e80f906SNeil Armstrong }
5079e80f906SNeil Armstrong 
5089e80f906SNeil Armstrong static void sx150x_irq_bus_lock(struct irq_data *d)
5099e80f906SNeil Armstrong {
5109e80f906SNeil Armstrong 	struct sx150x_pinctrl *pctl =
5119e80f906SNeil Armstrong 			gpiochip_get_data(irq_data_get_irq_chip_data(d));
5129e80f906SNeil Armstrong 
5139e80f906SNeil Armstrong 	mutex_lock(&pctl->lock);
5149e80f906SNeil Armstrong }
5159e80f906SNeil Armstrong 
5169e80f906SNeil Armstrong static void sx150x_irq_bus_sync_unlock(struct irq_data *d)
5179e80f906SNeil Armstrong {
5189e80f906SNeil Armstrong 	struct sx150x_pinctrl *pctl =
5199e80f906SNeil Armstrong 			gpiochip_get_data(irq_data_get_irq_chip_data(d));
5209e80f906SNeil Armstrong 
521*6489677fSAndrey Smirnov 	regmap_write(pctl->regmap, pctl->data->reg_irq_mask, pctl->irq.masked);
522*6489677fSAndrey Smirnov 	regmap_write(pctl->regmap, pctl->data->reg_sense, pctl->irq.sense);
5239e80f906SNeil Armstrong 	mutex_unlock(&pctl->lock);
5249e80f906SNeil Armstrong }
5259e80f906SNeil Armstrong 
5269e80f906SNeil Armstrong static int sx150x_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
5279e80f906SNeil Armstrong 			      unsigned long *config)
5289e80f906SNeil Armstrong {
5299e80f906SNeil Armstrong 	struct sx150x_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
5309e80f906SNeil Armstrong 	unsigned int param = pinconf_to_config_param(*config);
5319e80f906SNeil Armstrong 	int ret;
5329e80f906SNeil Armstrong 	u32 arg;
5330db0f26cSAndrey Smirnov 	unsigned int data;
5349e80f906SNeil Armstrong 
535*6489677fSAndrey Smirnov 	if (sx150x_pin_is_oscio(pctl, pin)) {
5369e80f906SNeil Armstrong 		switch (param) {
5379e80f906SNeil Armstrong 		case PIN_CONFIG_DRIVE_PUSH_PULL:
5389e80f906SNeil Armstrong 		case PIN_CONFIG_OUTPUT:
5399e80f906SNeil Armstrong 			mutex_lock(&pctl->lock);
5400db0f26cSAndrey Smirnov 			ret = regmap_read(pctl->regmap,
5419e80f906SNeil Armstrong 					  pctl->data->pri.x789.reg_clock,
5429e80f906SNeil Armstrong 					  &data);
5439e80f906SNeil Armstrong 			mutex_unlock(&pctl->lock);
5449e80f906SNeil Armstrong 
5459e80f906SNeil Armstrong 			if (ret < 0)
5469e80f906SNeil Armstrong 				return ret;
5479e80f906SNeil Armstrong 
5489e80f906SNeil Armstrong 			if (param == PIN_CONFIG_DRIVE_PUSH_PULL)
5499e80f906SNeil Armstrong 				arg = (data & 0x1f) ? 1 : 0;
5509e80f906SNeil Armstrong 			else {
5519e80f906SNeil Armstrong 				if ((data & 0x1f) == 0x1f)
5529e80f906SNeil Armstrong 					arg = 1;
5539e80f906SNeil Armstrong 				else if ((data & 0x1f) == 0x10)
5549e80f906SNeil Armstrong 					arg = 0;
5559e80f906SNeil Armstrong 				else
5569e80f906SNeil Armstrong 					return -EINVAL;
5579e80f906SNeil Armstrong 			}
5589e80f906SNeil Armstrong 
5599e80f906SNeil Armstrong 			break;
5609e80f906SNeil Armstrong 		default:
5619e80f906SNeil Armstrong 			return -ENOTSUPP;
5629e80f906SNeil Armstrong 		}
5639e80f906SNeil Armstrong 
5649e80f906SNeil Armstrong 		goto out;
5659e80f906SNeil Armstrong 	}
5669e80f906SNeil Armstrong 
5679e80f906SNeil Armstrong 	switch (param) {
5689e80f906SNeil Armstrong 	case PIN_CONFIG_BIAS_PULL_DOWN:
5699e80f906SNeil Armstrong 		mutex_lock(&pctl->lock);
570*6489677fSAndrey Smirnov 		ret = regmap_read(pctl->regmap,
571*6489677fSAndrey Smirnov 				  pctl->data->reg_pulldn,
572*6489677fSAndrey Smirnov 				  &data);
573*6489677fSAndrey Smirnov 		data &= BIT(pin);
5749e80f906SNeil Armstrong 		mutex_unlock(&pctl->lock);
5759e80f906SNeil Armstrong 
5769e80f906SNeil Armstrong 		if (ret < 0)
5779e80f906SNeil Armstrong 			return ret;
5789e80f906SNeil Armstrong 
5799e80f906SNeil Armstrong 		if (!ret)
5809e80f906SNeil Armstrong 			return -EINVAL;
5819e80f906SNeil Armstrong 
5829e80f906SNeil Armstrong 		arg = 1;
5839e80f906SNeil Armstrong 		break;
5849e80f906SNeil Armstrong 
5859e80f906SNeil Armstrong 	case PIN_CONFIG_BIAS_PULL_UP:
5869e80f906SNeil Armstrong 		mutex_lock(&pctl->lock);
587*6489677fSAndrey Smirnov 		ret = regmap_read(pctl->regmap,
588*6489677fSAndrey Smirnov 				  pctl->data->reg_pullup,
589*6489677fSAndrey Smirnov 				  &data);
590*6489677fSAndrey Smirnov 		data &= BIT(pin);
5919e80f906SNeil Armstrong 		mutex_unlock(&pctl->lock);
5929e80f906SNeil Armstrong 
5939e80f906SNeil Armstrong 		if (ret < 0)
5949e80f906SNeil Armstrong 			return ret;
5959e80f906SNeil Armstrong 
5969e80f906SNeil Armstrong 		if (!ret)
5979e80f906SNeil Armstrong 			return -EINVAL;
5989e80f906SNeil Armstrong 
5999e80f906SNeil Armstrong 		arg = 1;
6009e80f906SNeil Armstrong 		break;
6019e80f906SNeil Armstrong 
6029e80f906SNeil Armstrong 	case PIN_CONFIG_DRIVE_OPEN_DRAIN:
6039e80f906SNeil Armstrong 		if (pctl->data->model != SX150X_789)
6049e80f906SNeil Armstrong 			return -ENOTSUPP;
6059e80f906SNeil Armstrong 
6069e80f906SNeil Armstrong 		mutex_lock(&pctl->lock);
607*6489677fSAndrey Smirnov 		ret = regmap_read(pctl->regmap,
608*6489677fSAndrey Smirnov 				  pctl->data->pri.x789.reg_drain,
609*6489677fSAndrey Smirnov 				  &data);
610*6489677fSAndrey Smirnov 		data &= BIT(pin);
6119e80f906SNeil Armstrong 		mutex_unlock(&pctl->lock);
6129e80f906SNeil Armstrong 
6139e80f906SNeil Armstrong 		if (ret < 0)
6149e80f906SNeil Armstrong 			return ret;
6159e80f906SNeil Armstrong 
616*6489677fSAndrey Smirnov 		if (!data)
6179e80f906SNeil Armstrong 			return -EINVAL;
6189e80f906SNeil Armstrong 
6199e80f906SNeil Armstrong 		arg = 1;
6209e80f906SNeil Armstrong 		break;
6219e80f906SNeil Armstrong 
6229e80f906SNeil Armstrong 	case PIN_CONFIG_DRIVE_PUSH_PULL:
6239e80f906SNeil Armstrong 		if (pctl->data->model != SX150X_789)
6249e80f906SNeil Armstrong 			arg = true;
6259e80f906SNeil Armstrong 		else {
6269e80f906SNeil Armstrong 			mutex_lock(&pctl->lock);
627*6489677fSAndrey Smirnov 			ret = regmap_read(pctl->regmap,
628*6489677fSAndrey Smirnov 					  pctl->data->pri.x789.reg_drain,
629*6489677fSAndrey Smirnov 					  &data);
630*6489677fSAndrey Smirnov 			data &= BIT(pin);
6319e80f906SNeil Armstrong 			mutex_unlock(&pctl->lock);
6329e80f906SNeil Armstrong 
6339e80f906SNeil Armstrong 			if (ret < 0)
6349e80f906SNeil Armstrong 				return ret;
6359e80f906SNeil Armstrong 
636*6489677fSAndrey Smirnov 			if (data)
6379e80f906SNeil Armstrong 				return -EINVAL;
6389e80f906SNeil Armstrong 
6399e80f906SNeil Armstrong 			arg = 1;
6409e80f906SNeil Armstrong 		}
6419e80f906SNeil Armstrong 		break;
6429e80f906SNeil Armstrong 
6439e80f906SNeil Armstrong 	case PIN_CONFIG_OUTPUT:
6449e80f906SNeil Armstrong 		ret = sx150x_gpio_get_direction(&pctl->gpio, pin);
6459e80f906SNeil Armstrong 		if (ret < 0)
6469e80f906SNeil Armstrong 			return ret;
6479e80f906SNeil Armstrong 
6489e80f906SNeil Armstrong 		if (ret)
6499e80f906SNeil Armstrong 			return -EINVAL;
6509e80f906SNeil Armstrong 
6519e80f906SNeil Armstrong 		ret = sx150x_gpio_get(&pctl->gpio, pin);
6529e80f906SNeil Armstrong 		if (ret < 0)
6539e80f906SNeil Armstrong 			return ret;
6549e80f906SNeil Armstrong 
6559e80f906SNeil Armstrong 		arg = ret;
6569e80f906SNeil Armstrong 		break;
6579e80f906SNeil Armstrong 
6589e80f906SNeil Armstrong 	default:
6599e80f906SNeil Armstrong 		return -ENOTSUPP;
6609e80f906SNeil Armstrong 	}
6619e80f906SNeil Armstrong 
6629e80f906SNeil Armstrong out:
6639e80f906SNeil Armstrong 	*config = pinconf_to_config_packed(param, arg);
6649e80f906SNeil Armstrong 
6659e80f906SNeil Armstrong 	return 0;
6669e80f906SNeil Armstrong }
6679e80f906SNeil Armstrong 
6689e80f906SNeil Armstrong static int sx150x_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
6699e80f906SNeil Armstrong 			      unsigned long *configs, unsigned int num_configs)
6709e80f906SNeil Armstrong {
6719e80f906SNeil Armstrong 	struct sx150x_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
6729e80f906SNeil Armstrong 	enum pin_config_param param;
6739e80f906SNeil Armstrong 	u32 arg;
6749e80f906SNeil Armstrong 	int i;
6759e80f906SNeil Armstrong 	int ret;
6769e80f906SNeil Armstrong 
6779e80f906SNeil Armstrong 	for (i = 0; i < num_configs; i++) {
6789e80f906SNeil Armstrong 		param = pinconf_to_config_param(configs[i]);
6799e80f906SNeil Armstrong 		arg = pinconf_to_config_argument(configs[i]);
6809e80f906SNeil Armstrong 
6819e80f906SNeil Armstrong 		if (sx150x_pin_is_oscio(pctl, pin)) {
6829e80f906SNeil Armstrong 			if (param == PIN_CONFIG_OUTPUT) {
6839e80f906SNeil Armstrong 				ret = sx150x_gpio_direction_output(&pctl->gpio,
6849e80f906SNeil Armstrong 								   pin, arg);
6859e80f906SNeil Armstrong 				if (ret < 0)
6869e80f906SNeil Armstrong 					return ret;
6879e80f906SNeil Armstrong 
6889e80f906SNeil Armstrong 				continue;
6899e80f906SNeil Armstrong 			} else
6909e80f906SNeil Armstrong 				return -ENOTSUPP;
6919e80f906SNeil Armstrong 		}
6929e80f906SNeil Armstrong 
6939e80f906SNeil Armstrong 		switch (param) {
6949e80f906SNeil Armstrong 		case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
6959e80f906SNeil Armstrong 		case PIN_CONFIG_BIAS_DISABLE:
6969e80f906SNeil Armstrong 			mutex_lock(&pctl->lock);
697*6489677fSAndrey Smirnov 			ret = regmap_write_bits(pctl->regmap,
698*6489677fSAndrey Smirnov 						pctl->data->reg_pulldn,
699*6489677fSAndrey Smirnov 						BIT(pin), 0);
7009e80f906SNeil Armstrong 			mutex_unlock(&pctl->lock);
7019e80f906SNeil Armstrong 			if (ret < 0)
7029e80f906SNeil Armstrong 				return ret;
7039e80f906SNeil Armstrong 
7049e80f906SNeil Armstrong 			mutex_lock(&pctl->lock);
705*6489677fSAndrey Smirnov 			ret = regmap_write_bits(pctl->regmap,
706*6489677fSAndrey Smirnov 						pctl->data->reg_pullup,
707*6489677fSAndrey Smirnov 						BIT(pin), 0);
7089e80f906SNeil Armstrong 			mutex_unlock(&pctl->lock);
7099e80f906SNeil Armstrong 			if (ret < 0)
7109e80f906SNeil Armstrong 				return ret;
7119e80f906SNeil Armstrong 
7129e80f906SNeil Armstrong 			break;
7139e80f906SNeil Armstrong 
7149e80f906SNeil Armstrong 		case PIN_CONFIG_BIAS_PULL_UP:
7159e80f906SNeil Armstrong 			mutex_lock(&pctl->lock);
716*6489677fSAndrey Smirnov 			ret = regmap_write_bits(pctl->regmap,
7179e80f906SNeil Armstrong 						pctl->data->reg_pullup,
718*6489677fSAndrey Smirnov 						BIT(pin), BIT(pin));
7199e80f906SNeil Armstrong 			mutex_unlock(&pctl->lock);
7209e80f906SNeil Armstrong 			if (ret < 0)
7219e80f906SNeil Armstrong 				return ret;
7229e80f906SNeil Armstrong 
7239e80f906SNeil Armstrong 			break;
7249e80f906SNeil Armstrong 
7259e80f906SNeil Armstrong 		case PIN_CONFIG_BIAS_PULL_DOWN:
7269e80f906SNeil Armstrong 			mutex_lock(&pctl->lock);
727*6489677fSAndrey Smirnov 			ret = regmap_write_bits(pctl->regmap,
7289e80f906SNeil Armstrong 						pctl->data->reg_pulldn,
729*6489677fSAndrey Smirnov 						BIT(pin), BIT(pin));
7309e80f906SNeil Armstrong 			mutex_unlock(&pctl->lock);
7319e80f906SNeil Armstrong 			if (ret < 0)
7329e80f906SNeil Armstrong 				return ret;
7339e80f906SNeil Armstrong 
7349e80f906SNeil Armstrong 			break;
7359e80f906SNeil Armstrong 
7369e80f906SNeil Armstrong 		case PIN_CONFIG_DRIVE_OPEN_DRAIN:
7379e80f906SNeil Armstrong 			ret = sx150x_gpio_set_single_ended(&pctl->gpio,
7389e80f906SNeil Armstrong 						pin, LINE_MODE_OPEN_DRAIN);
7399e80f906SNeil Armstrong 			if (ret < 0)
7409e80f906SNeil Armstrong 				return ret;
7419e80f906SNeil Armstrong 
7429e80f906SNeil Armstrong 			break;
7439e80f906SNeil Armstrong 
7449e80f906SNeil Armstrong 		case PIN_CONFIG_DRIVE_PUSH_PULL:
7459e80f906SNeil Armstrong 			ret = sx150x_gpio_set_single_ended(&pctl->gpio,
7469e80f906SNeil Armstrong 						pin, LINE_MODE_PUSH_PULL);
7479e80f906SNeil Armstrong 			if (ret < 0)
7489e80f906SNeil Armstrong 				return ret;
7499e80f906SNeil Armstrong 
7509e80f906SNeil Armstrong 			break;
7519e80f906SNeil Armstrong 
7529e80f906SNeil Armstrong 		case PIN_CONFIG_OUTPUT:
7539e80f906SNeil Armstrong 			ret = sx150x_gpio_direction_output(&pctl->gpio,
7549e80f906SNeil Armstrong 							   pin, arg);
7559e80f906SNeil Armstrong 			if (ret < 0)
7569e80f906SNeil Armstrong 				return ret;
7579e80f906SNeil Armstrong 
7589e80f906SNeil Armstrong 			break;
7599e80f906SNeil Armstrong 
7609e80f906SNeil Armstrong 		default:
7619e80f906SNeil Armstrong 			return -ENOTSUPP;
7629e80f906SNeil Armstrong 		}
7639e80f906SNeil Armstrong 	} /* for each config */
7649e80f906SNeil Armstrong 
7659e80f906SNeil Armstrong 	return 0;
7669e80f906SNeil Armstrong }
7679e80f906SNeil Armstrong 
7689e80f906SNeil Armstrong static const struct pinconf_ops sx150x_pinconf_ops = {
7699e80f906SNeil Armstrong 	.pin_config_get = sx150x_pinconf_get,
7709e80f906SNeil Armstrong 	.pin_config_set = sx150x_pinconf_set,
7719e80f906SNeil Armstrong 	.is_generic = true,
7729e80f906SNeil Armstrong };
7739e80f906SNeil Armstrong 
7749e80f906SNeil Armstrong static const struct i2c_device_id sx150x_id[] = {
7759e80f906SNeil Armstrong 	{"sx1508q", (kernel_ulong_t) &sx1508q_device_data },
7769e80f906SNeil Armstrong 	{"sx1509q", (kernel_ulong_t) &sx1509q_device_data },
7779e80f906SNeil Armstrong 	{"sx1506q", (kernel_ulong_t) &sx1506q_device_data },
7789e80f906SNeil Armstrong 	{"sx1502q", (kernel_ulong_t) &sx1502q_device_data },
7796697546dSAndrey Smirnov 	{"sx1503q", (kernel_ulong_t) &sx1503q_device_data },
7809e80f906SNeil Armstrong 	{}
7819e80f906SNeil Armstrong };
7829e80f906SNeil Armstrong 
7839e80f906SNeil Armstrong static const struct of_device_id sx150x_of_match[] = {
784e3ba8120SAndrey Smirnov 	{ .compatible = "semtech,sx1508q", .data = &sx1508q_device_data },
785e3ba8120SAndrey Smirnov 	{ .compatible = "semtech,sx1509q", .data = &sx1509q_device_data },
786e3ba8120SAndrey Smirnov 	{ .compatible = "semtech,sx1506q", .data = &sx1506q_device_data },
787e3ba8120SAndrey Smirnov 	{ .compatible = "semtech,sx1502q", .data = &sx1502q_device_data },
7886697546dSAndrey Smirnov 	{ .compatible = "semtech,sx1503q", .data = &sx1503q_device_data },
7899e80f906SNeil Armstrong 	{},
7909e80f906SNeil Armstrong };
7919e80f906SNeil Armstrong 
7929e80f906SNeil Armstrong static int sx150x_reset(struct sx150x_pinctrl *pctl)
7939e80f906SNeil Armstrong {
7949e80f906SNeil Armstrong 	int err;
7959e80f906SNeil Armstrong 
7969e80f906SNeil Armstrong 	err = i2c_smbus_write_byte_data(pctl->client,
7979e80f906SNeil Armstrong 					pctl->data->pri.x789.reg_reset,
7989e80f906SNeil Armstrong 					0x12);
7999e80f906SNeil Armstrong 	if (err < 0)
8009e80f906SNeil Armstrong 		return err;
8019e80f906SNeil Armstrong 
8029e80f906SNeil Armstrong 	err = i2c_smbus_write_byte_data(pctl->client,
8039e80f906SNeil Armstrong 					pctl->data->pri.x789.reg_reset,
8049e80f906SNeil Armstrong 					0x34);
8059e80f906SNeil Armstrong 	return err;
8069e80f906SNeil Armstrong }
8079e80f906SNeil Armstrong 
808310cdfa0SAndrey Smirnov static int sx150x_init_misc(struct sx150x_pinctrl *pctl)
809310cdfa0SAndrey Smirnov {
810310cdfa0SAndrey Smirnov 	u8 reg, value;
811310cdfa0SAndrey Smirnov 
812310cdfa0SAndrey Smirnov 	switch (pctl->data->model) {
813310cdfa0SAndrey Smirnov 	case SX150X_789:
814310cdfa0SAndrey Smirnov 		reg   = pctl->data->pri.x789.reg_misc;
815310cdfa0SAndrey Smirnov 		value = SX150X_789_REG_MISC_AUTOCLEAR_OFF;
816310cdfa0SAndrey Smirnov 		break;
817310cdfa0SAndrey Smirnov 	case SX150X_456:
818310cdfa0SAndrey Smirnov 		reg   = pctl->data->pri.x456.reg_advance;
819310cdfa0SAndrey Smirnov 		value = 0x00;
820b30d31e4SAndrey Smirnov 
821b30d31e4SAndrey Smirnov 		/*
822b30d31e4SAndrey Smirnov 		 * Only SX1506 has RegAdvanced, SX1504/5 are expected
823b30d31e4SAndrey Smirnov 		 * to initialize this offset to zero
824b30d31e4SAndrey Smirnov 		 */
825b30d31e4SAndrey Smirnov 		if (!reg)
826b30d31e4SAndrey Smirnov 			return 0;
827310cdfa0SAndrey Smirnov 		break;
828310cdfa0SAndrey Smirnov 	case SX150X_123:
829310cdfa0SAndrey Smirnov 		reg   = pctl->data->pri.x123.reg_advance;
830310cdfa0SAndrey Smirnov 		value = 0x00;
831310cdfa0SAndrey Smirnov 		break;
832310cdfa0SAndrey Smirnov 	default:
833310cdfa0SAndrey Smirnov 		WARN(1, "Unknown chip model %d\n", pctl->data->model);
834310cdfa0SAndrey Smirnov 		return -EINVAL;
835310cdfa0SAndrey Smirnov 	}
836310cdfa0SAndrey Smirnov 
837*6489677fSAndrey Smirnov 	return regmap_write(pctl->regmap, reg, value);
838310cdfa0SAndrey Smirnov }
839310cdfa0SAndrey Smirnov 
8409e80f906SNeil Armstrong static int sx150x_init_hw(struct sx150x_pinctrl *pctl)
8419e80f906SNeil Armstrong {
842*6489677fSAndrey Smirnov 	const u8 reg[] = {
843*6489677fSAndrey Smirnov 		[SX150X_789] = pctl->data->pri.x789.reg_polarity,
844*6489677fSAndrey Smirnov 		[SX150X_456] = pctl->data->pri.x456.reg_pld_mode,
845*6489677fSAndrey Smirnov 		[SX150X_123] = pctl->data->pri.x123.reg_pld_mode,
846*6489677fSAndrey Smirnov 	};
8479e80f906SNeil Armstrong 	int err;
8489e80f906SNeil Armstrong 
8499e80f906SNeil Armstrong 	if (pctl->data->model == SX150X_789 &&
8509e80f906SNeil Armstrong 	    of_property_read_bool(pctl->dev->of_node, "semtech,probe-reset")) {
8519e80f906SNeil Armstrong 		err = sx150x_reset(pctl);
8529e80f906SNeil Armstrong 		if (err < 0)
8539e80f906SNeil Armstrong 			return err;
8549e80f906SNeil Armstrong 	}
8559e80f906SNeil Armstrong 
856310cdfa0SAndrey Smirnov 	err = sx150x_init_misc(pctl);
8579e80f906SNeil Armstrong 	if (err < 0)
8589e80f906SNeil Armstrong 		return err;
8599e80f906SNeil Armstrong 
8609e80f906SNeil Armstrong 	/* Set all pins to work in normal mode */
861*6489677fSAndrey Smirnov 	return regmap_write(pctl->regmap, reg[pctl->data->model], 0);
8629e80f906SNeil Armstrong }
8639e80f906SNeil Armstrong 
864*6489677fSAndrey Smirnov static int sx150x_regmap_reg_width(struct sx150x_pinctrl *pctl,
865*6489677fSAndrey Smirnov 				   unsigned int reg)
866*6489677fSAndrey Smirnov {
867*6489677fSAndrey Smirnov 	const struct sx150x_device_data *data = pctl->data;
868*6489677fSAndrey Smirnov 
869*6489677fSAndrey Smirnov 	if (reg == data->reg_sense) {
870*6489677fSAndrey Smirnov 		/*
871*6489677fSAndrey Smirnov 		 * RegSense packs two bits of configuration per GPIO,
872*6489677fSAndrey Smirnov 		 * so we'd need to read twice as many bits as there
873*6489677fSAndrey Smirnov 		 * are GPIO in our chip
874*6489677fSAndrey Smirnov 		 */
875*6489677fSAndrey Smirnov 		return 2 * data->ngpios;
876*6489677fSAndrey Smirnov 	} else if ((data->model == SX150X_789 &&
877*6489677fSAndrey Smirnov 		    (reg == data->pri.x789.reg_misc ||
878*6489677fSAndrey Smirnov 		     reg == data->pri.x789.reg_clock ||
879*6489677fSAndrey Smirnov 		     reg == data->pri.x789.reg_reset))
880*6489677fSAndrey Smirnov 		   ||
881*6489677fSAndrey Smirnov 		   (data->model == SX150X_123 &&
882*6489677fSAndrey Smirnov 		    reg == data->pri.x123.reg_advance)
883*6489677fSAndrey Smirnov 		   ||
884*6489677fSAndrey Smirnov 		   (data->model == SX150X_456 &&
885*6489677fSAndrey Smirnov 		    reg == data->pri.x456.reg_advance)) {
886*6489677fSAndrey Smirnov 		return 8;
887*6489677fSAndrey Smirnov 	} else {
888*6489677fSAndrey Smirnov 		return data->ngpios;
889*6489677fSAndrey Smirnov 	}
890*6489677fSAndrey Smirnov }
891*6489677fSAndrey Smirnov 
892*6489677fSAndrey Smirnov static unsigned int sx150x_maybe_swizzle(struct sx150x_pinctrl *pctl,
893*6489677fSAndrey Smirnov 					 unsigned int reg, unsigned int val)
894*6489677fSAndrey Smirnov {
895*6489677fSAndrey Smirnov 	unsigned int a, b;
896*6489677fSAndrey Smirnov 	const struct sx150x_device_data *data = pctl->data;
897*6489677fSAndrey Smirnov 
898*6489677fSAndrey Smirnov 	/*
899*6489677fSAndrey Smirnov 	 * Whereas SX1509 presents RegSense in a simple layout as such:
900*6489677fSAndrey Smirnov 	 *	reg     [ f f e e d d c c ]
901*6489677fSAndrey Smirnov 	 *	reg + 1 [ b b a a 9 9 8 8 ]
902*6489677fSAndrey Smirnov 	 *	reg + 2 [ 7 7 6 6 5 5 4 4 ]
903*6489677fSAndrey Smirnov 	 *	reg + 3 [ 3 3 2 2 1 1 0 0 ]
904*6489677fSAndrey Smirnov 	 *
905*6489677fSAndrey Smirnov 	 * SX1503 and SX1506 deviate from that data layout, instead storing
906*6489677fSAndrey Smirnov 	 * thier contents as follows:
907*6489677fSAndrey Smirnov 	 *
908*6489677fSAndrey Smirnov 	 *	reg     [ f f e e d d c c ]
909*6489677fSAndrey Smirnov 	 *	reg + 1 [ 7 7 6 6 5 5 4 4 ]
910*6489677fSAndrey Smirnov 	 *	reg + 2 [ b b a a 9 9 8 8 ]
911*6489677fSAndrey Smirnov 	 *	reg + 3 [ 3 3 2 2 1 1 0 0 ]
912*6489677fSAndrey Smirnov 	 *
913*6489677fSAndrey Smirnov 	 * so, taking that into account, we swap two
914*6489677fSAndrey Smirnov 	 * inner bytes of a 4-byte result
915*6489677fSAndrey Smirnov 	 */
916*6489677fSAndrey Smirnov 
917*6489677fSAndrey Smirnov 	if (reg == data->reg_sense &&
918*6489677fSAndrey Smirnov 	    data->ngpios == 16 &&
919*6489677fSAndrey Smirnov 	    (data->model == SX150X_123 ||
920*6489677fSAndrey Smirnov 	     data->model == SX150X_456)) {
921*6489677fSAndrey Smirnov 		a = val & 0x00ff0000;
922*6489677fSAndrey Smirnov 		b = val & 0x0000ff00;
923*6489677fSAndrey Smirnov 
924*6489677fSAndrey Smirnov 		val &= 0xff0000ff;
925*6489677fSAndrey Smirnov 		val |= b << 8;
926*6489677fSAndrey Smirnov 		val |= a >> 8;
927*6489677fSAndrey Smirnov 	}
928*6489677fSAndrey Smirnov 
929*6489677fSAndrey Smirnov 	return val;
930*6489677fSAndrey Smirnov }
931*6489677fSAndrey Smirnov 
932*6489677fSAndrey Smirnov /*
933*6489677fSAndrey Smirnov  * In order to mask the differences between 16 and 8 bit expander
934*6489677fSAndrey Smirnov  * devices we set up a sligthly ficticious regmap that pretends to be
935*6489677fSAndrey Smirnov  * a set of 32-bit (to accomodate RegSenseLow/RegSenseHigh
936*6489677fSAndrey Smirnov  * pair/quartet) registers and transparently reconstructs those
937*6489677fSAndrey Smirnov  * registers via multiple I2C/SMBus reads
938*6489677fSAndrey Smirnov  *
939*6489677fSAndrey Smirnov  * This way the rest of the driver code, interfacing with the chip via
940*6489677fSAndrey Smirnov  * regmap API, can work assuming that each GPIO pin is represented by
941*6489677fSAndrey Smirnov  * a group of bits at an offset proportioan to GPIO number within a
942*6489677fSAndrey Smirnov  * given register.
943*6489677fSAndrey Smirnov  *
944*6489677fSAndrey Smirnov  */
945*6489677fSAndrey Smirnov static int sx150x_regmap_reg_read(void *context, unsigned int reg,
946*6489677fSAndrey Smirnov 				  unsigned int *result)
947*6489677fSAndrey Smirnov {
948*6489677fSAndrey Smirnov 	int ret, n;
949*6489677fSAndrey Smirnov 	struct sx150x_pinctrl *pctl = context;
950*6489677fSAndrey Smirnov 	struct i2c_client *i2c = pctl->client;
951*6489677fSAndrey Smirnov 	const int width = sx150x_regmap_reg_width(pctl, reg);
952*6489677fSAndrey Smirnov 	unsigned int idx, val;
953*6489677fSAndrey Smirnov 
954*6489677fSAndrey Smirnov 	/*
955*6489677fSAndrey Smirnov 	 * There are four potential cases coverd by this function:
956*6489677fSAndrey Smirnov 	 *
957*6489677fSAndrey Smirnov 	 * 1) 8-pin chip, single configuration bit register
958*6489677fSAndrey Smirnov 	 *
959*6489677fSAndrey Smirnov 	 *	This is trivial the code below just needs to read:
960*6489677fSAndrey Smirnov 	 *		reg  [ 7 6 5 4 3 2 1 0 ]
961*6489677fSAndrey Smirnov 	 *
962*6489677fSAndrey Smirnov 	 * 2) 8-pin chip, double configuration bit register (RegSense)
963*6489677fSAndrey Smirnov 	 *
964*6489677fSAndrey Smirnov 	 *	The read will be done as follows:
965*6489677fSAndrey Smirnov 	 *		reg      [ 7 7 6 6 5 5 4 4 ]
966*6489677fSAndrey Smirnov 	 *		reg + 1  [ 3 3 2 2 1 1 0 0 ]
967*6489677fSAndrey Smirnov 	 *
968*6489677fSAndrey Smirnov 	 * 3) 16-pin chip, single configuration bit register
969*6489677fSAndrey Smirnov 	 *
970*6489677fSAndrey Smirnov 	 *	The read will be done as follows:
971*6489677fSAndrey Smirnov 	 *		reg     [ f e d c b a 9 8 ]
972*6489677fSAndrey Smirnov 	 *		reg + 1 [ 7 6 5 4 3 2 1 0 ]
973*6489677fSAndrey Smirnov 	 *
974*6489677fSAndrey Smirnov 	 * 4) 16-pin chip, double configuration bit register (RegSense)
975*6489677fSAndrey Smirnov 	 *
976*6489677fSAndrey Smirnov 	 *	The read will be done as follows:
977*6489677fSAndrey Smirnov 	 *		reg     [ f f e e d d c c ]
978*6489677fSAndrey Smirnov 	 *		reg + 1 [ b b a a 9 9 8 8 ]
979*6489677fSAndrey Smirnov 	 *		reg + 2 [ 7 7 6 6 5 5 4 4 ]
980*6489677fSAndrey Smirnov 	 *		reg + 3 [ 3 3 2 2 1 1 0 0 ]
981*6489677fSAndrey Smirnov 	 */
982*6489677fSAndrey Smirnov 
983*6489677fSAndrey Smirnov 	for (n = width, val = 0, idx = reg; n > 0; n -= 8, idx++) {
984*6489677fSAndrey Smirnov 		val <<= 8;
985*6489677fSAndrey Smirnov 
986*6489677fSAndrey Smirnov 		ret = i2c_smbus_read_byte_data(i2c, idx);
987*6489677fSAndrey Smirnov 		if (ret < 0)
988*6489677fSAndrey Smirnov 			return ret;
989*6489677fSAndrey Smirnov 
990*6489677fSAndrey Smirnov 		val |= ret;
991*6489677fSAndrey Smirnov 	}
992*6489677fSAndrey Smirnov 
993*6489677fSAndrey Smirnov 	*result = sx150x_maybe_swizzle(pctl, reg, val);
994*6489677fSAndrey Smirnov 
995*6489677fSAndrey Smirnov 	return 0;
996*6489677fSAndrey Smirnov }
997*6489677fSAndrey Smirnov 
998*6489677fSAndrey Smirnov static int sx150x_regmap_reg_write(void *context, unsigned int reg,
999*6489677fSAndrey Smirnov 				   unsigned int val)
1000*6489677fSAndrey Smirnov {
1001*6489677fSAndrey Smirnov 	int ret, n;
1002*6489677fSAndrey Smirnov 	struct sx150x_pinctrl *pctl = context;
1003*6489677fSAndrey Smirnov 	struct i2c_client *i2c = pctl->client;
1004*6489677fSAndrey Smirnov 	const int width = sx150x_regmap_reg_width(pctl, reg);
1005*6489677fSAndrey Smirnov 
1006*6489677fSAndrey Smirnov 	val = sx150x_maybe_swizzle(pctl, reg, val);
1007*6489677fSAndrey Smirnov 
1008*6489677fSAndrey Smirnov 	n = width - 8;
1009*6489677fSAndrey Smirnov 	do {
1010*6489677fSAndrey Smirnov 		const u8 byte = (val >> n) & 0xff;
1011*6489677fSAndrey Smirnov 
1012*6489677fSAndrey Smirnov 		ret = i2c_smbus_write_byte_data(i2c, reg, byte);
1013*6489677fSAndrey Smirnov 		if (ret < 0)
1014*6489677fSAndrey Smirnov 			return ret;
1015*6489677fSAndrey Smirnov 
1016*6489677fSAndrey Smirnov 		reg++;
1017*6489677fSAndrey Smirnov 		n -= 8;
1018*6489677fSAndrey Smirnov 	} while (n >= 0);
1019*6489677fSAndrey Smirnov 
10209e80f906SNeil Armstrong 	return 0;
10219e80f906SNeil Armstrong }
10229e80f906SNeil Armstrong 
10230db0f26cSAndrey Smirnov static bool sx150x_reg_volatile(struct device *dev, unsigned int reg)
10240db0f26cSAndrey Smirnov {
10250db0f26cSAndrey Smirnov 	struct sx150x_pinctrl *pctl = i2c_get_clientdata(to_i2c_client(dev));
10260db0f26cSAndrey Smirnov 
1027*6489677fSAndrey Smirnov 	return reg == pctl->data->reg_irq_src || reg == pctl->data->reg_data;
10280db0f26cSAndrey Smirnov }
10290db0f26cSAndrey Smirnov 
10300db0f26cSAndrey Smirnov const struct regmap_config sx150x_regmap_config = {
10310db0f26cSAndrey Smirnov 	.reg_bits = 8,
1032*6489677fSAndrey Smirnov 	.val_bits = 32,
10330db0f26cSAndrey Smirnov 
10340db0f26cSAndrey Smirnov 	.cache_type = REGCACHE_RBTREE,
10350db0f26cSAndrey Smirnov 
1036*6489677fSAndrey Smirnov 	.reg_read = sx150x_regmap_reg_read,
1037*6489677fSAndrey Smirnov 	.reg_write = sx150x_regmap_reg_write,
1038*6489677fSAndrey Smirnov 
10390db0f26cSAndrey Smirnov 	.max_register = SX150X_MAX_REGISTER,
10400db0f26cSAndrey Smirnov 	.volatile_reg = sx150x_reg_volatile,
10410db0f26cSAndrey Smirnov };
10420db0f26cSAndrey Smirnov 
10439e80f906SNeil Armstrong static int sx150x_probe(struct i2c_client *client,
10449e80f906SNeil Armstrong 			const struct i2c_device_id *id)
10459e80f906SNeil Armstrong {
10469e80f906SNeil Armstrong 	static const u32 i2c_funcs = I2C_FUNC_SMBUS_BYTE_DATA |
10479e80f906SNeil Armstrong 				     I2C_FUNC_SMBUS_WRITE_WORD_DATA;
10489e80f906SNeil Armstrong 	struct device *dev = &client->dev;
10499e80f906SNeil Armstrong 	struct sx150x_pinctrl *pctl;
10509e80f906SNeil Armstrong 	int ret;
10519e80f906SNeil Armstrong 
10529e80f906SNeil Armstrong 	if (!i2c_check_functionality(client->adapter, i2c_funcs))
10539e80f906SNeil Armstrong 		return -ENOSYS;
10549e80f906SNeil Armstrong 
10559e80f906SNeil Armstrong 	pctl = devm_kzalloc(dev, sizeof(*pctl), GFP_KERNEL);
10569e80f906SNeil Armstrong 	if (!pctl)
10579e80f906SNeil Armstrong 		return -ENOMEM;
10589e80f906SNeil Armstrong 
10590db0f26cSAndrey Smirnov 	i2c_set_clientdata(client, pctl);
10600db0f26cSAndrey Smirnov 
10619e80f906SNeil Armstrong 	pctl->dev = dev;
10629e80f906SNeil Armstrong 	pctl->client = client;
1063e3ba8120SAndrey Smirnov 
1064e3ba8120SAndrey Smirnov 	if (dev->of_node)
1065e3ba8120SAndrey Smirnov 		pctl->data = of_device_get_match_data(dev);
1066e3ba8120SAndrey Smirnov 	else
1067e3ba8120SAndrey Smirnov 		pctl->data = (struct sx150x_device_data *)id->driver_data;
1068e3ba8120SAndrey Smirnov 
1069e3ba8120SAndrey Smirnov 	if (!pctl->data)
1070e3ba8120SAndrey Smirnov 		return -EINVAL;
10719e80f906SNeil Armstrong 
1072*6489677fSAndrey Smirnov 	pctl->regmap = devm_regmap_init(dev, NULL, pctl,
1073*6489677fSAndrey Smirnov 					&sx150x_regmap_config);
10740db0f26cSAndrey Smirnov 	if (IS_ERR(pctl->regmap)) {
10750db0f26cSAndrey Smirnov 		ret = PTR_ERR(pctl->regmap);
10760db0f26cSAndrey Smirnov 		dev_err(dev, "Failed to allocate register map: %d\n",
10770db0f26cSAndrey Smirnov 			ret);
10780db0f26cSAndrey Smirnov 		return ret;
10790db0f26cSAndrey Smirnov 	}
10800db0f26cSAndrey Smirnov 
10819e80f906SNeil Armstrong 	mutex_init(&pctl->lock);
10829e80f906SNeil Armstrong 
10839e80f906SNeil Armstrong 	ret = sx150x_init_hw(pctl);
10849e80f906SNeil Armstrong 	if (ret)
10859e80f906SNeil Armstrong 		return ret;
10869e80f906SNeil Armstrong 
10879e80f906SNeil Armstrong 	/* Register GPIO controller */
10889e80f906SNeil Armstrong 	pctl->gpio.label = devm_kstrdup(dev, client->name, GFP_KERNEL);
10899e80f906SNeil Armstrong 	pctl->gpio.base = -1;
10909e80f906SNeil Armstrong 	pctl->gpio.ngpio = pctl->data->npins;
10919e80f906SNeil Armstrong 	pctl->gpio.get_direction = sx150x_gpio_get_direction;
10929e80f906SNeil Armstrong 	pctl->gpio.direction_input = sx150x_gpio_direction_input;
10939e80f906SNeil Armstrong 	pctl->gpio.direction_output = sx150x_gpio_direction_output;
10949e80f906SNeil Armstrong 	pctl->gpio.get = sx150x_gpio_get;
10959e80f906SNeil Armstrong 	pctl->gpio.set = sx150x_gpio_set;
10969e80f906SNeil Armstrong 	pctl->gpio.set_single_ended = sx150x_gpio_set_single_ended;
10979e80f906SNeil Armstrong 	pctl->gpio.parent = dev;
10989e80f906SNeil Armstrong #ifdef CONFIG_OF_GPIO
10999e80f906SNeil Armstrong 	pctl->gpio.of_node = dev->of_node;
11009e80f906SNeil Armstrong #endif
11019e80f906SNeil Armstrong 	pctl->gpio.can_sleep = true;
11029e80f906SNeil Armstrong 
11039e80f906SNeil Armstrong 	ret = devm_gpiochip_add_data(dev, &pctl->gpio, pctl);
11049e80f906SNeil Armstrong 	if (ret)
11059e80f906SNeil Armstrong 		return ret;
11069e80f906SNeil Armstrong 
11079e80f906SNeil Armstrong 	/* Add Interrupt support if an irq is specified */
11089e80f906SNeil Armstrong 	if (client->irq > 0) {
11099e80f906SNeil Armstrong 		pctl->irq_chip.name = devm_kstrdup(dev, client->name,
11109e80f906SNeil Armstrong 						   GFP_KERNEL);
11119e80f906SNeil Armstrong 		pctl->irq_chip.irq_mask = sx150x_irq_mask;
11129e80f906SNeil Armstrong 		pctl->irq_chip.irq_unmask = sx150x_irq_unmask;
11139e80f906SNeil Armstrong 		pctl->irq_chip.irq_set_type = sx150x_irq_set_type;
11149e80f906SNeil Armstrong 		pctl->irq_chip.irq_bus_lock = sx150x_irq_bus_lock;
11159e80f906SNeil Armstrong 		pctl->irq_chip.irq_bus_sync_unlock = sx150x_irq_bus_sync_unlock;
11169e80f906SNeil Armstrong 
11179e80f906SNeil Armstrong 		pctl->irq.masked = ~0;
11189e80f906SNeil Armstrong 		pctl->irq.sense = 0;
11199e80f906SNeil Armstrong 
11209e80f906SNeil Armstrong 		ret = gpiochip_irqchip_add(&pctl->gpio,
11219e80f906SNeil Armstrong 					   &pctl->irq_chip, 0,
11229e80f906SNeil Armstrong 					   handle_edge_irq, IRQ_TYPE_NONE);
11239e80f906SNeil Armstrong 		if (ret) {
11249e80f906SNeil Armstrong 			dev_err(dev, "could not connect irqchip to gpiochip\n");
11259e80f906SNeil Armstrong 			return ret;
11269e80f906SNeil Armstrong 		}
11279e80f906SNeil Armstrong 
11289e80f906SNeil Armstrong 		ret = devm_request_threaded_irq(dev, client->irq, NULL,
11299e80f906SNeil Armstrong 						sx150x_irq_thread_fn,
11309e80f906SNeil Armstrong 						IRQF_ONESHOT | IRQF_SHARED |
11319e80f906SNeil Armstrong 						IRQF_TRIGGER_FALLING,
11329e80f906SNeil Armstrong 						pctl->irq_chip.name, pctl);
11339e80f906SNeil Armstrong 		if (ret < 0)
11349e80f906SNeil Armstrong 			return ret;
11359e80f906SNeil Armstrong 	}
11369e80f906SNeil Armstrong 
11379e80f906SNeil Armstrong 	/* Pinctrl_desc */
11389e80f906SNeil Armstrong 	pctl->pinctrl_desc.name = "sx150x-pinctrl";
11399e80f906SNeil Armstrong 	pctl->pinctrl_desc.pctlops = &sx150x_pinctrl_ops;
11409e80f906SNeil Armstrong 	pctl->pinctrl_desc.confops = &sx150x_pinconf_ops;
11419e80f906SNeil Armstrong 	pctl->pinctrl_desc.pins = pctl->data->pins;
11429e80f906SNeil Armstrong 	pctl->pinctrl_desc.npins = pctl->data->npins;
11439e80f906SNeil Armstrong 	pctl->pinctrl_desc.owner = THIS_MODULE;
11449e80f906SNeil Armstrong 
11459e80f906SNeil Armstrong 	pctl->pctldev = pinctrl_register(&pctl->pinctrl_desc, dev, pctl);
11469e80f906SNeil Armstrong 	if (IS_ERR(pctl->pctldev)) {
11479e80f906SNeil Armstrong 		dev_err(dev, "Failed to register pinctrl device\n");
11489e80f906SNeil Armstrong 		return PTR_ERR(pctl->pctldev);
11499e80f906SNeil Armstrong 	}
11509e80f906SNeil Armstrong 
11519e80f906SNeil Armstrong 	return 0;
11529e80f906SNeil Armstrong }
11539e80f906SNeil Armstrong 
11549e80f906SNeil Armstrong static struct i2c_driver sx150x_driver = {
11559e80f906SNeil Armstrong 	.driver = {
11569e80f906SNeil Armstrong 		.name = "sx150x-pinctrl",
11579e80f906SNeil Armstrong 		.of_match_table = of_match_ptr(sx150x_of_match),
11589e80f906SNeil Armstrong 	},
11599e80f906SNeil Armstrong 	.probe    = sx150x_probe,
11609e80f906SNeil Armstrong 	.id_table = sx150x_id,
11619e80f906SNeil Armstrong };
11629e80f906SNeil Armstrong 
11639e80f906SNeil Armstrong static int __init sx150x_init(void)
11649e80f906SNeil Armstrong {
11659e80f906SNeil Armstrong 	return i2c_add_driver(&sx150x_driver);
11669e80f906SNeil Armstrong }
11679e80f906SNeil Armstrong subsys_initcall(sx150x_init);
1168