xref: /openbmc/linux/drivers/pinctrl/pinctrl-sx150x.c (revision 4f5ac8cf0a1121144432fd0175fac9ec31f58cd0)
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>
30fc669d13SLinus Walleij #include <linux/gpio/driver.h>
319e80f906SNeil Armstrong #include <linux/pinctrl/pinconf.h>
329e80f906SNeil Armstrong #include <linux/pinctrl/pinctrl.h>
339e80f906SNeil Armstrong #include <linux/pinctrl/pinmux.h>
349e80f906SNeil Armstrong #include <linux/pinctrl/pinconf-generic.h>
359e80f906SNeil Armstrong 
369e80f906SNeil Armstrong #include "core.h"
379e80f906SNeil Armstrong #include "pinconf.h"
389e80f906SNeil Armstrong #include "pinctrl-utils.h"
399e80f906SNeil Armstrong 
409e80f906SNeil Armstrong /* The chip models of sx150x */
419e80f906SNeil Armstrong enum {
429e80f906SNeil Armstrong 	SX150X_123 = 0,
439e80f906SNeil Armstrong 	SX150X_456,
449e80f906SNeil Armstrong 	SX150X_789,
459e80f906SNeil Armstrong };
467d68a79aSAndrey Smirnov enum {
477d68a79aSAndrey Smirnov 	SX150X_789_REG_MISC_AUTOCLEAR_OFF = 1 << 0,
480db0f26cSAndrey Smirnov 	SX150X_MAX_REGISTER = 0xad,
49fd931f23SAndrey Smirnov 	SX150X_IRQ_TYPE_EDGE_RISING = 0x1,
50fd931f23SAndrey Smirnov 	SX150X_IRQ_TYPE_EDGE_FALLING = 0x2,
51f9038d60SAndrey Smirnov 	SX150X_789_RESET_KEY1 = 0x12,
52f9038d60SAndrey Smirnov 	SX150X_789_RESET_KEY2 = 0x34,
537d68a79aSAndrey Smirnov };
549e80f906SNeil Armstrong 
559e80f906SNeil Armstrong struct sx150x_123_pri {
569e80f906SNeil Armstrong 	u8 reg_pld_mode;
579e80f906SNeil Armstrong 	u8 reg_pld_table0;
589e80f906SNeil Armstrong 	u8 reg_pld_table1;
599e80f906SNeil Armstrong 	u8 reg_pld_table2;
609e80f906SNeil Armstrong 	u8 reg_pld_table3;
619e80f906SNeil Armstrong 	u8 reg_pld_table4;
629e80f906SNeil Armstrong 	u8 reg_advance;
639e80f906SNeil Armstrong };
649e80f906SNeil Armstrong 
659e80f906SNeil Armstrong struct sx150x_456_pri {
669e80f906SNeil Armstrong 	u8 reg_pld_mode;
679e80f906SNeil Armstrong 	u8 reg_pld_table0;
689e80f906SNeil Armstrong 	u8 reg_pld_table1;
699e80f906SNeil Armstrong 	u8 reg_pld_table2;
709e80f906SNeil Armstrong 	u8 reg_pld_table3;
719e80f906SNeil Armstrong 	u8 reg_pld_table4;
729e80f906SNeil Armstrong 	u8 reg_advance;
739e80f906SNeil Armstrong };
749e80f906SNeil Armstrong 
759e80f906SNeil Armstrong struct sx150x_789_pri {
769e80f906SNeil Armstrong 	u8 reg_drain;
779e80f906SNeil Armstrong 	u8 reg_polarity;
789e80f906SNeil Armstrong 	u8 reg_clock;
799e80f906SNeil Armstrong 	u8 reg_misc;
809e80f906SNeil Armstrong 	u8 reg_reset;
819e80f906SNeil Armstrong 	u8 ngpios;
829e80f906SNeil Armstrong };
839e80f906SNeil Armstrong 
849e80f906SNeil Armstrong struct sx150x_device_data {
859e80f906SNeil Armstrong 	u8 model;
869e80f906SNeil Armstrong 	u8 reg_pullup;
879e80f906SNeil Armstrong 	u8 reg_pulldn;
889e80f906SNeil Armstrong 	u8 reg_dir;
899e80f906SNeil Armstrong 	u8 reg_data;
909e80f906SNeil Armstrong 	u8 reg_irq_mask;
919e80f906SNeil Armstrong 	u8 reg_irq_src;
929e80f906SNeil Armstrong 	u8 reg_sense;
939e80f906SNeil Armstrong 	u8 ngpios;
949e80f906SNeil Armstrong 	union {
959e80f906SNeil Armstrong 		struct sx150x_123_pri x123;
969e80f906SNeil Armstrong 		struct sx150x_456_pri x456;
979e80f906SNeil Armstrong 		struct sx150x_789_pri x789;
989e80f906SNeil Armstrong 	} pri;
999e80f906SNeil Armstrong 	const struct pinctrl_pin_desc *pins;
1009e80f906SNeil Armstrong 	unsigned int npins;
1019e80f906SNeil Armstrong };
1029e80f906SNeil Armstrong 
1039e80f906SNeil Armstrong struct sx150x_pinctrl {
1049e80f906SNeil Armstrong 	struct device *dev;
1059e80f906SNeil Armstrong 	struct i2c_client *client;
1069e80f906SNeil Armstrong 	struct pinctrl_dev *pctldev;
1079e80f906SNeil Armstrong 	struct pinctrl_desc pinctrl_desc;
1089e80f906SNeil Armstrong 	struct gpio_chip gpio;
1099e80f906SNeil Armstrong 	struct irq_chip irq_chip;
1100db0f26cSAndrey Smirnov 	struct regmap *regmap;
1119e80f906SNeil Armstrong 	struct {
1129e80f906SNeil Armstrong 		u32 sense;
1139e80f906SNeil Armstrong 		u32 masked;
1149e80f906SNeil Armstrong 	} irq;
1159e80f906SNeil Armstrong 	struct mutex lock;
1169e80f906SNeil Armstrong 	const struct sx150x_device_data *data;
1179e80f906SNeil Armstrong };
1189e80f906SNeil Armstrong 
119*4f5ac8cfSPeter Rosin static const struct pinctrl_pin_desc sx150x_4_pins[] = {
120*4f5ac8cfSPeter Rosin 	PINCTRL_PIN(0, "gpio0"),
121*4f5ac8cfSPeter Rosin 	PINCTRL_PIN(1, "gpio1"),
122*4f5ac8cfSPeter Rosin 	PINCTRL_PIN(2, "gpio2"),
123*4f5ac8cfSPeter Rosin 	PINCTRL_PIN(3, "gpio3"),
124*4f5ac8cfSPeter Rosin 	PINCTRL_PIN(4, "oscio"),
125*4f5ac8cfSPeter Rosin };
126*4f5ac8cfSPeter Rosin 
1279e80f906SNeil Armstrong static const struct pinctrl_pin_desc sx150x_8_pins[] = {
1289e80f906SNeil Armstrong 	PINCTRL_PIN(0, "gpio0"),
1299e80f906SNeil Armstrong 	PINCTRL_PIN(1, "gpio1"),
1309e80f906SNeil Armstrong 	PINCTRL_PIN(2, "gpio2"),
1319e80f906SNeil Armstrong 	PINCTRL_PIN(3, "gpio3"),
1329e80f906SNeil Armstrong 	PINCTRL_PIN(4, "gpio4"),
1339e80f906SNeil Armstrong 	PINCTRL_PIN(5, "gpio5"),
1349e80f906SNeil Armstrong 	PINCTRL_PIN(6, "gpio6"),
1359e80f906SNeil Armstrong 	PINCTRL_PIN(7, "gpio7"),
1369e80f906SNeil Armstrong 	PINCTRL_PIN(8, "oscio"),
1379e80f906SNeil Armstrong };
1389e80f906SNeil Armstrong 
1399e80f906SNeil Armstrong static const struct pinctrl_pin_desc sx150x_16_pins[] = {
1409e80f906SNeil Armstrong 	PINCTRL_PIN(0, "gpio0"),
1419e80f906SNeil Armstrong 	PINCTRL_PIN(1, "gpio1"),
1429e80f906SNeil Armstrong 	PINCTRL_PIN(2, "gpio2"),
1439e80f906SNeil Armstrong 	PINCTRL_PIN(3, "gpio3"),
1449e80f906SNeil Armstrong 	PINCTRL_PIN(4, "gpio4"),
1459e80f906SNeil Armstrong 	PINCTRL_PIN(5, "gpio5"),
1469e80f906SNeil Armstrong 	PINCTRL_PIN(6, "gpio6"),
1479e80f906SNeil Armstrong 	PINCTRL_PIN(7, "gpio7"),
1489e80f906SNeil Armstrong 	PINCTRL_PIN(8, "gpio8"),
1499e80f906SNeil Armstrong 	PINCTRL_PIN(9, "gpio9"),
1509e80f906SNeil Armstrong 	PINCTRL_PIN(10, "gpio10"),
1519e80f906SNeil Armstrong 	PINCTRL_PIN(11, "gpio11"),
1529e80f906SNeil Armstrong 	PINCTRL_PIN(12, "gpio12"),
1539e80f906SNeil Armstrong 	PINCTRL_PIN(13, "gpio13"),
1549e80f906SNeil Armstrong 	PINCTRL_PIN(14, "gpio14"),
1559e80f906SNeil Armstrong 	PINCTRL_PIN(15, "gpio15"),
1569e80f906SNeil Armstrong 	PINCTRL_PIN(16, "oscio"),
1579e80f906SNeil Armstrong };
1589e80f906SNeil Armstrong 
159*4f5ac8cfSPeter Rosin static const struct sx150x_device_data sx1501q_device_data = {
160*4f5ac8cfSPeter Rosin 	.model = SX150X_123,
161*4f5ac8cfSPeter Rosin 	.reg_pullup	= 0x02,
162*4f5ac8cfSPeter Rosin 	.reg_pulldn	= 0x03,
163*4f5ac8cfSPeter Rosin 	.reg_dir	= 0x01,
164*4f5ac8cfSPeter Rosin 	.reg_data	= 0x00,
165*4f5ac8cfSPeter Rosin 	.reg_irq_mask	= 0x05,
166*4f5ac8cfSPeter Rosin 	.reg_irq_src	= 0x08,
167*4f5ac8cfSPeter Rosin 	.reg_sense	= 0x07,
168*4f5ac8cfSPeter Rosin 	.pri.x123 = {
169*4f5ac8cfSPeter Rosin 		.reg_pld_mode	= 0x10,
170*4f5ac8cfSPeter Rosin 		.reg_pld_table0	= 0x11,
171*4f5ac8cfSPeter Rosin 		.reg_pld_table2	= 0x13,
172*4f5ac8cfSPeter Rosin 		.reg_advance	= 0xad,
173*4f5ac8cfSPeter Rosin 	},
174*4f5ac8cfSPeter Rosin 	.ngpios	= 4,
175*4f5ac8cfSPeter Rosin 	.pins = sx150x_4_pins,
176*4f5ac8cfSPeter Rosin 	.npins = 4, /* oscio not available */
177*4f5ac8cfSPeter Rosin };
178*4f5ac8cfSPeter Rosin 
1799e80f906SNeil Armstrong static const struct sx150x_device_data sx1502q_device_data = {
1809e80f906SNeil Armstrong 	.model = SX150X_123,
1819e80f906SNeil Armstrong 	.reg_pullup	= 0x02,
1829e80f906SNeil Armstrong 	.reg_pulldn	= 0x03,
1839e80f906SNeil Armstrong 	.reg_dir	= 0x01,
1849e80f906SNeil Armstrong 	.reg_data	= 0x00,
1859e80f906SNeil Armstrong 	.reg_irq_mask	= 0x05,
1869e80f906SNeil Armstrong 	.reg_irq_src	= 0x08,
1871663682cSPeter Rosin 	.reg_sense	= 0x06,
1889e80f906SNeil Armstrong 	.pri.x123 = {
1899e80f906SNeil Armstrong 		.reg_pld_mode	= 0x10,
1909e80f906SNeil Armstrong 		.reg_pld_table0	= 0x11,
1919e80f906SNeil Armstrong 		.reg_pld_table1	= 0x12,
1929e80f906SNeil Armstrong 		.reg_pld_table2	= 0x13,
1939e80f906SNeil Armstrong 		.reg_pld_table3	= 0x14,
1949e80f906SNeil Armstrong 		.reg_pld_table4	= 0x15,
1959e80f906SNeil Armstrong 		.reg_advance	= 0xad,
1969e80f906SNeil Armstrong 	},
1979e80f906SNeil Armstrong 	.ngpios	= 8,
1989e80f906SNeil Armstrong 	.pins = sx150x_8_pins,
1999e80f906SNeil Armstrong 	.npins = 8, /* oscio not available */
2009e80f906SNeil Armstrong };
2019e80f906SNeil Armstrong 
2026697546dSAndrey Smirnov static const struct sx150x_device_data sx1503q_device_data = {
2036697546dSAndrey Smirnov 	.model = SX150X_123,
2046489677fSAndrey Smirnov 	.reg_pullup	= 0x04,
2056489677fSAndrey Smirnov 	.reg_pulldn	= 0x06,
2066489677fSAndrey Smirnov 	.reg_dir	= 0x02,
2076489677fSAndrey Smirnov 	.reg_data	= 0x00,
2086489677fSAndrey Smirnov 	.reg_irq_mask	= 0x08,
2096489677fSAndrey Smirnov 	.reg_irq_src	= 0x0e,
2106489677fSAndrey Smirnov 	.reg_sense	= 0x0a,
2116697546dSAndrey Smirnov 	.pri.x123 = {
2126489677fSAndrey Smirnov 		.reg_pld_mode	= 0x20,
2136489677fSAndrey Smirnov 		.reg_pld_table0	= 0x22,
2146489677fSAndrey Smirnov 		.reg_pld_table1	= 0x24,
2156489677fSAndrey Smirnov 		.reg_pld_table2	= 0x26,
2166489677fSAndrey Smirnov 		.reg_pld_table3	= 0x28,
2176489677fSAndrey Smirnov 		.reg_pld_table4	= 0x2a,
2186697546dSAndrey Smirnov 		.reg_advance	= 0xad,
2196697546dSAndrey Smirnov 	},
2206697546dSAndrey Smirnov 	.ngpios	= 16,
2216697546dSAndrey Smirnov 	.pins = sx150x_16_pins,
2226697546dSAndrey Smirnov 	.npins  = 16, /* oscio not available */
2236697546dSAndrey Smirnov };
2246697546dSAndrey Smirnov 
225*4f5ac8cfSPeter Rosin static const struct sx150x_device_data sx1504q_device_data = {
226*4f5ac8cfSPeter Rosin 	.model = SX150X_456,
227*4f5ac8cfSPeter Rosin 	.reg_pullup	= 0x02,
228*4f5ac8cfSPeter Rosin 	.reg_pulldn	= 0x03,
229*4f5ac8cfSPeter Rosin 	.reg_dir	= 0x01,
230*4f5ac8cfSPeter Rosin 	.reg_data	= 0x00,
231*4f5ac8cfSPeter Rosin 	.reg_irq_mask	= 0x05,
232*4f5ac8cfSPeter Rosin 	.reg_irq_src	= 0x08,
233*4f5ac8cfSPeter Rosin 	.reg_sense	= 0x07,
234*4f5ac8cfSPeter Rosin 	.pri.x456 = {
235*4f5ac8cfSPeter Rosin 		.reg_pld_mode	= 0x10,
236*4f5ac8cfSPeter Rosin 		.reg_pld_table0	= 0x11,
237*4f5ac8cfSPeter Rosin 		.reg_pld_table2	= 0x13,
238*4f5ac8cfSPeter Rosin 	},
239*4f5ac8cfSPeter Rosin 	.ngpios	= 4,
240*4f5ac8cfSPeter Rosin 	.pins = sx150x_4_pins,
241*4f5ac8cfSPeter Rosin 	.npins = 4, /* oscio not available */
242*4f5ac8cfSPeter Rosin };
243*4f5ac8cfSPeter Rosin 
244*4f5ac8cfSPeter Rosin static const struct sx150x_device_data sx1505q_device_data = {
245*4f5ac8cfSPeter Rosin 	.model = SX150X_456,
246*4f5ac8cfSPeter Rosin 	.reg_pullup	= 0x02,
247*4f5ac8cfSPeter Rosin 	.reg_pulldn	= 0x03,
248*4f5ac8cfSPeter Rosin 	.reg_dir	= 0x01,
249*4f5ac8cfSPeter Rosin 	.reg_data	= 0x00,
250*4f5ac8cfSPeter Rosin 	.reg_irq_mask	= 0x05,
251*4f5ac8cfSPeter Rosin 	.reg_irq_src	= 0x08,
252*4f5ac8cfSPeter Rosin 	.reg_sense	= 0x06,
253*4f5ac8cfSPeter Rosin 	.pri.x456 = {
254*4f5ac8cfSPeter Rosin 		.reg_pld_mode	= 0x10,
255*4f5ac8cfSPeter Rosin 		.reg_pld_table0	= 0x11,
256*4f5ac8cfSPeter Rosin 		.reg_pld_table1	= 0x12,
257*4f5ac8cfSPeter Rosin 		.reg_pld_table2	= 0x13,
258*4f5ac8cfSPeter Rosin 		.reg_pld_table3	= 0x14,
259*4f5ac8cfSPeter Rosin 		.reg_pld_table4	= 0x15,
260*4f5ac8cfSPeter Rosin 	},
261*4f5ac8cfSPeter Rosin 	.ngpios	= 8,
262*4f5ac8cfSPeter Rosin 	.pins = sx150x_8_pins,
263*4f5ac8cfSPeter Rosin 	.npins = 8, /* oscio not available */
264*4f5ac8cfSPeter Rosin };
265*4f5ac8cfSPeter Rosin 
266bba709bdSPeter Rosin static const struct sx150x_device_data sx1506q_device_data = {
267bba709bdSPeter Rosin 	.model = SX150X_456,
268bba709bdSPeter Rosin 	.reg_pullup	= 0x04,
269bba709bdSPeter Rosin 	.reg_pulldn	= 0x06,
270bba709bdSPeter Rosin 	.reg_dir	= 0x02,
271bba709bdSPeter Rosin 	.reg_data	= 0x00,
272bba709bdSPeter Rosin 	.reg_irq_mask	= 0x08,
273bba709bdSPeter Rosin 	.reg_irq_src	= 0x0e,
274bba709bdSPeter Rosin 	.reg_sense	= 0x0a,
275bba709bdSPeter Rosin 	.pri.x456 = {
276bba709bdSPeter Rosin 		.reg_pld_mode	= 0x20,
277bba709bdSPeter Rosin 		.reg_pld_table0	= 0x22,
278bba709bdSPeter Rosin 		.reg_pld_table1	= 0x24,
279bba709bdSPeter Rosin 		.reg_pld_table2	= 0x26,
280bba709bdSPeter Rosin 		.reg_pld_table3	= 0x28,
281bba709bdSPeter Rosin 		.reg_pld_table4	= 0x2a,
282bba709bdSPeter Rosin 		.reg_advance	= 0xad,
283bba709bdSPeter Rosin 	},
284bba709bdSPeter Rosin 	.ngpios	= 16,
285bba709bdSPeter Rosin 	.pins = sx150x_16_pins,
286bba709bdSPeter Rosin 	.npins = 16, /* oscio not available */
287bba709bdSPeter Rosin };
288bba709bdSPeter Rosin 
289*4f5ac8cfSPeter Rosin static const struct sx150x_device_data sx1507q_device_data = {
290*4f5ac8cfSPeter Rosin 	.model = SX150X_789,
291*4f5ac8cfSPeter Rosin 	.reg_pullup	= 0x03,
292*4f5ac8cfSPeter Rosin 	.reg_pulldn	= 0x04,
293*4f5ac8cfSPeter Rosin 	.reg_dir	= 0x07,
294*4f5ac8cfSPeter Rosin 	.reg_data	= 0x08,
295*4f5ac8cfSPeter Rosin 	.reg_irq_mask	= 0x09,
296*4f5ac8cfSPeter Rosin 	.reg_irq_src	= 0x0b,
297*4f5ac8cfSPeter Rosin 	.reg_sense	= 0x0a,
298*4f5ac8cfSPeter Rosin 	.pri.x789 = {
299*4f5ac8cfSPeter Rosin 		.reg_drain	= 0x05,
300*4f5ac8cfSPeter Rosin 		.reg_polarity	= 0x06,
301*4f5ac8cfSPeter Rosin 		.reg_clock	= 0x0d,
302*4f5ac8cfSPeter Rosin 		.reg_misc	= 0x0e,
303*4f5ac8cfSPeter Rosin 		.reg_reset	= 0x7d,
304*4f5ac8cfSPeter Rosin 	},
305*4f5ac8cfSPeter Rosin 	.ngpios = 4,
306*4f5ac8cfSPeter Rosin 	.pins = sx150x_4_pins,
307*4f5ac8cfSPeter Rosin 	.npins = ARRAY_SIZE(sx150x_4_pins),
308*4f5ac8cfSPeter Rosin };
309*4f5ac8cfSPeter Rosin 
310bba709bdSPeter Rosin static const struct sx150x_device_data sx1508q_device_data = {
311bba709bdSPeter Rosin 	.model = SX150X_789,
312bba709bdSPeter Rosin 	.reg_pullup	= 0x03,
313bba709bdSPeter Rosin 	.reg_pulldn	= 0x04,
314bba709bdSPeter Rosin 	.reg_dir	= 0x07,
315bba709bdSPeter Rosin 	.reg_data	= 0x08,
316bba709bdSPeter Rosin 	.reg_irq_mask	= 0x09,
317bba709bdSPeter Rosin 	.reg_irq_src	= 0x0c,
318bba709bdSPeter Rosin 	.reg_sense	= 0x0a,
319bba709bdSPeter Rosin 	.pri.x789 = {
320bba709bdSPeter Rosin 		.reg_drain	= 0x05,
321bba709bdSPeter Rosin 		.reg_polarity	= 0x06,
322bba709bdSPeter Rosin 		.reg_clock	= 0x0f,
323bba709bdSPeter Rosin 		.reg_misc	= 0x10,
324bba709bdSPeter Rosin 		.reg_reset	= 0x7d,
325bba709bdSPeter Rosin 	},
326bba709bdSPeter Rosin 	.ngpios = 8,
327bba709bdSPeter Rosin 	.pins = sx150x_8_pins,
328bba709bdSPeter Rosin 	.npins = ARRAY_SIZE(sx150x_8_pins),
329bba709bdSPeter Rosin };
330bba709bdSPeter Rosin 
331bba709bdSPeter Rosin static const struct sx150x_device_data sx1509q_device_data = {
332bba709bdSPeter Rosin 	.model = SX150X_789,
333bba709bdSPeter Rosin 	.reg_pullup	= 0x06,
334bba709bdSPeter Rosin 	.reg_pulldn	= 0x08,
335bba709bdSPeter Rosin 	.reg_dir	= 0x0e,
336bba709bdSPeter Rosin 	.reg_data	= 0x10,
337bba709bdSPeter Rosin 	.reg_irq_mask	= 0x12,
338bba709bdSPeter Rosin 	.reg_irq_src	= 0x18,
339bba709bdSPeter Rosin 	.reg_sense	= 0x14,
340bba709bdSPeter Rosin 	.pri.x789 = {
341bba709bdSPeter Rosin 		.reg_drain	= 0x0a,
342bba709bdSPeter Rosin 		.reg_polarity	= 0x0c,
343bba709bdSPeter Rosin 		.reg_clock	= 0x1e,
344bba709bdSPeter Rosin 		.reg_misc	= 0x1f,
345bba709bdSPeter Rosin 		.reg_reset	= 0x7d,
346bba709bdSPeter Rosin 	},
347bba709bdSPeter Rosin 	.ngpios	= 16,
348bba709bdSPeter Rosin 	.pins = sx150x_16_pins,
349bba709bdSPeter Rosin 	.npins = ARRAY_SIZE(sx150x_16_pins),
350bba709bdSPeter Rosin };
351bba709bdSPeter Rosin 
3529e80f906SNeil Armstrong static int sx150x_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
3539e80f906SNeil Armstrong {
3549e80f906SNeil Armstrong 	return 0;
3559e80f906SNeil Armstrong }
3569e80f906SNeil Armstrong 
3579e80f906SNeil Armstrong static const char *sx150x_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
3589e80f906SNeil Armstrong 						unsigned int group)
3599e80f906SNeil Armstrong {
3609e80f906SNeil Armstrong 	return NULL;
3619e80f906SNeil Armstrong }
3629e80f906SNeil Armstrong 
3639e80f906SNeil Armstrong static int sx150x_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
3649e80f906SNeil Armstrong 					unsigned int group,
3659e80f906SNeil Armstrong 					const unsigned int **pins,
3669e80f906SNeil Armstrong 					unsigned int *num_pins)
3679e80f906SNeil Armstrong {
3689e80f906SNeil Armstrong 	return -ENOTSUPP;
3699e80f906SNeil Armstrong }
3709e80f906SNeil Armstrong 
3719e80f906SNeil Armstrong static const struct pinctrl_ops sx150x_pinctrl_ops = {
3729e80f906SNeil Armstrong 	.get_groups_count = sx150x_pinctrl_get_groups_count,
3739e80f906SNeil Armstrong 	.get_group_name = sx150x_pinctrl_get_group_name,
3749e80f906SNeil Armstrong 	.get_group_pins = sx150x_pinctrl_get_group_pins,
3759e80f906SNeil Armstrong #ifdef CONFIG_OF
3769e80f906SNeil Armstrong 	.dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
3779e80f906SNeil Armstrong 	.dt_free_map = pinctrl_utils_free_map,
3789e80f906SNeil Armstrong #endif
3799e80f906SNeil Armstrong };
3809e80f906SNeil Armstrong 
3819e80f906SNeil Armstrong static bool sx150x_pin_is_oscio(struct sx150x_pinctrl *pctl, unsigned int pin)
3829e80f906SNeil Armstrong {
3839e80f906SNeil Armstrong 	if (pin >= pctl->data->npins)
3849e80f906SNeil Armstrong 		return false;
3859e80f906SNeil Armstrong 
3869e80f906SNeil Armstrong 	/* OSCIO pin is only present in 789 devices */
3879e80f906SNeil Armstrong 	if (pctl->data->model != SX150X_789)
3889e80f906SNeil Armstrong 		return false;
3899e80f906SNeil Armstrong 
3909e80f906SNeil Armstrong 	return !strcmp(pctl->data->pins[pin].name, "oscio");
3919e80f906SNeil Armstrong }
3929e80f906SNeil Armstrong 
3939e80f906SNeil Armstrong static int sx150x_gpio_get_direction(struct gpio_chip *chip,
3949e80f906SNeil Armstrong 				      unsigned int offset)
3959e80f906SNeil Armstrong {
3969e80f906SNeil Armstrong 	struct sx150x_pinctrl *pctl = gpiochip_get_data(chip);
3976489677fSAndrey Smirnov 	unsigned int value;
3986489677fSAndrey Smirnov 	int ret;
3999e80f906SNeil Armstrong 
4009e80f906SNeil Armstrong 	if (sx150x_pin_is_oscio(pctl, offset))
4019e80f906SNeil Armstrong 		return false;
4029e80f906SNeil Armstrong 
4036489677fSAndrey Smirnov 	ret = regmap_read(pctl->regmap, pctl->data->reg_dir, &value);
4046489677fSAndrey Smirnov 	if (ret < 0)
4056489677fSAndrey Smirnov 		return ret;
4069e80f906SNeil Armstrong 
4076489677fSAndrey Smirnov 	return !!(value & BIT(offset));
4089e80f906SNeil Armstrong }
4099e80f906SNeil Armstrong 
4109e80f906SNeil Armstrong static int sx150x_gpio_get(struct gpio_chip *chip, unsigned int offset)
4119e80f906SNeil Armstrong {
4129e80f906SNeil Armstrong 	struct sx150x_pinctrl *pctl = gpiochip_get_data(chip);
4136489677fSAndrey Smirnov 	unsigned int value;
4146489677fSAndrey Smirnov 	int ret;
4159e80f906SNeil Armstrong 
4169e80f906SNeil Armstrong 	if (sx150x_pin_is_oscio(pctl, offset))
4179e80f906SNeil Armstrong 		return -EINVAL;
4189e80f906SNeil Armstrong 
4196489677fSAndrey Smirnov 	ret = regmap_read(pctl->regmap, pctl->data->reg_data, &value);
4206489677fSAndrey Smirnov 	if (ret < 0)
4216489677fSAndrey Smirnov 		return ret;
4229e80f906SNeil Armstrong 
4236489677fSAndrey Smirnov 	return !!(value & BIT(offset));
4249e80f906SNeil Armstrong }
4259e80f906SNeil Armstrong 
4269e80f906SNeil Armstrong static int sx150x_gpio_set_single_ended(struct gpio_chip *chip,
4279e80f906SNeil Armstrong 					unsigned int offset,
4289e80f906SNeil Armstrong 					enum single_ended_mode mode)
4299e80f906SNeil Armstrong {
4309e80f906SNeil Armstrong 	struct sx150x_pinctrl *pctl = gpiochip_get_data(chip);
4319e80f906SNeil Armstrong 	int ret;
4329e80f906SNeil Armstrong 
4339e80f906SNeil Armstrong 	switch (mode) {
4349e80f906SNeil Armstrong 	case LINE_MODE_PUSH_PULL:
4359e80f906SNeil Armstrong 		if (pctl->data->model != SX150X_789 ||
4369e80f906SNeil Armstrong 		    sx150x_pin_is_oscio(pctl, offset))
4379e80f906SNeil Armstrong 			return 0;
4389e80f906SNeil Armstrong 
4396489677fSAndrey Smirnov 		ret = regmap_write_bits(pctl->regmap,
4409e80f906SNeil Armstrong 					pctl->data->pri.x789.reg_drain,
4416489677fSAndrey Smirnov 					BIT(offset), 0);
4429e80f906SNeil Armstrong 		break;
4439e80f906SNeil Armstrong 
4449e80f906SNeil Armstrong 	case LINE_MODE_OPEN_DRAIN:
4459e80f906SNeil Armstrong 		if (pctl->data->model != SX150X_789 ||
4469e80f906SNeil Armstrong 		    sx150x_pin_is_oscio(pctl, offset))
4479e80f906SNeil Armstrong 			return -ENOTSUPP;
4489e80f906SNeil Armstrong 
4496489677fSAndrey Smirnov 		ret = regmap_write_bits(pctl->regmap,
4509e80f906SNeil Armstrong 					pctl->data->pri.x789.reg_drain,
4516489677fSAndrey Smirnov 					BIT(offset), BIT(offset));
4529e80f906SNeil Armstrong 		break;
4539e80f906SNeil Armstrong 	default:
454d977a876SAndrey Smirnov 		ret = -ENOTSUPP;
455d977a876SAndrey Smirnov 		break;
4569e80f906SNeil Armstrong 	}
4579e80f906SNeil Armstrong 
458d977a876SAndrey Smirnov 	return ret;
4599e80f906SNeil Armstrong }
4609e80f906SNeil Armstrong 
4616489677fSAndrey Smirnov static int __sx150x_gpio_set(struct sx150x_pinctrl *pctl, unsigned int offset,
4626489677fSAndrey Smirnov 			     int value)
4636489677fSAndrey Smirnov {
4646489677fSAndrey Smirnov 	return regmap_write_bits(pctl->regmap, pctl->data->reg_data,
4656489677fSAndrey Smirnov 				 BIT(offset), value ? BIT(offset) : 0);
4666489677fSAndrey Smirnov }
4676489677fSAndrey Smirnov 
468ab5bd035SAndrey Smirnov static int sx150x_gpio_oscio_set(struct sx150x_pinctrl *pctl,
469ab5bd035SAndrey Smirnov 				 int value)
470ab5bd035SAndrey Smirnov {
471ab5bd035SAndrey Smirnov 	return regmap_write(pctl->regmap,
472ab5bd035SAndrey Smirnov 			    pctl->data->pri.x789.reg_clock,
473ab5bd035SAndrey Smirnov 			    (value ? 0x1f : 0x10));
474ab5bd035SAndrey Smirnov }
475ab5bd035SAndrey Smirnov 
4769e80f906SNeil Armstrong static void sx150x_gpio_set(struct gpio_chip *chip, unsigned int offset,
4779e80f906SNeil Armstrong 			    int value)
4789e80f906SNeil Armstrong {
4799e80f906SNeil Armstrong 	struct sx150x_pinctrl *pctl = gpiochip_get_data(chip);
4809e80f906SNeil Armstrong 
481d977a876SAndrey Smirnov 	if (sx150x_pin_is_oscio(pctl, offset))
482ab5bd035SAndrey Smirnov 		sx150x_gpio_oscio_set(pctl, value);
483d977a876SAndrey Smirnov 	else
4846489677fSAndrey Smirnov 		__sx150x_gpio_set(pctl, offset, value);
485d977a876SAndrey Smirnov 
4869e80f906SNeil Armstrong }
4879e80f906SNeil Armstrong 
488ec61168bSPeter Rosin static void sx150x_gpio_set_multiple(struct gpio_chip *chip,
489ec61168bSPeter Rosin 				     unsigned long *mask,
490ec61168bSPeter Rosin 				     unsigned long *bits)
491ec61168bSPeter Rosin {
492ec61168bSPeter Rosin 	struct sx150x_pinctrl *pctl = gpiochip_get_data(chip);
493ec61168bSPeter Rosin 
494ec61168bSPeter Rosin 	regmap_write_bits(pctl->regmap, pctl->data->reg_data, *mask, *bits);
495ec61168bSPeter Rosin }
496ec61168bSPeter Rosin 
4979e80f906SNeil Armstrong static int sx150x_gpio_direction_input(struct gpio_chip *chip,
4989e80f906SNeil Armstrong 				       unsigned int offset)
4999e80f906SNeil Armstrong {
5009e80f906SNeil Armstrong 	struct sx150x_pinctrl *pctl = gpiochip_get_data(chip);
5019e80f906SNeil Armstrong 
5029e80f906SNeil Armstrong 	if (sx150x_pin_is_oscio(pctl, offset))
5039e80f906SNeil Armstrong 		return -EINVAL;
5049e80f906SNeil Armstrong 
505d977a876SAndrey Smirnov 	return regmap_write_bits(pctl->regmap,
5066489677fSAndrey Smirnov 				 pctl->data->reg_dir,
5076489677fSAndrey Smirnov 				 BIT(offset), BIT(offset));
5089e80f906SNeil Armstrong }
5099e80f906SNeil Armstrong 
5109e80f906SNeil Armstrong static int sx150x_gpio_direction_output(struct gpio_chip *chip,
5119e80f906SNeil Armstrong 					unsigned int offset, int value)
5129e80f906SNeil Armstrong {
5139e80f906SNeil Armstrong 	struct sx150x_pinctrl *pctl = gpiochip_get_data(chip);
514d977a876SAndrey Smirnov 	int ret;
5159e80f906SNeil Armstrong 
516ab5bd035SAndrey Smirnov 	if (sx150x_pin_is_oscio(pctl, offset))
517ab5bd035SAndrey Smirnov 		return sx150x_gpio_oscio_set(pctl, value);
5189e80f906SNeil Armstrong 
519d977a876SAndrey Smirnov 	ret = __sx150x_gpio_set(pctl, offset, value);
520d977a876SAndrey Smirnov 	if (ret < 0)
521d977a876SAndrey Smirnov 		return ret;
522d977a876SAndrey Smirnov 
523d977a876SAndrey Smirnov 	return regmap_write_bits(pctl->regmap,
5246489677fSAndrey Smirnov 				 pctl->data->reg_dir,
5256489677fSAndrey Smirnov 				 BIT(offset), 0);
5269e80f906SNeil Armstrong }
5279e80f906SNeil Armstrong 
5289e80f906SNeil Armstrong static void sx150x_irq_mask(struct irq_data *d)
5299e80f906SNeil Armstrong {
5309e80f906SNeil Armstrong 	struct sx150x_pinctrl *pctl =
5319e80f906SNeil Armstrong 			gpiochip_get_data(irq_data_get_irq_chip_data(d));
5329e80f906SNeil Armstrong 	unsigned int n = d->hwirq;
5339e80f906SNeil Armstrong 
5346489677fSAndrey Smirnov 	pctl->irq.masked |= BIT(n);
5359e80f906SNeil Armstrong }
5369e80f906SNeil Armstrong 
5379e80f906SNeil Armstrong static void sx150x_irq_unmask(struct irq_data *d)
5389e80f906SNeil Armstrong {
5399e80f906SNeil Armstrong 	struct sx150x_pinctrl *pctl =
5409e80f906SNeil Armstrong 			gpiochip_get_data(irq_data_get_irq_chip_data(d));
5419e80f906SNeil Armstrong 	unsigned int n = d->hwirq;
5429e80f906SNeil Armstrong 
5436489677fSAndrey Smirnov 	pctl->irq.masked &= ~BIT(n);
5449e80f906SNeil Armstrong }
5459e80f906SNeil Armstrong 
546fd931f23SAndrey Smirnov static void sx150x_irq_set_sense(struct sx150x_pinctrl *pctl,
547fd931f23SAndrey Smirnov 				 unsigned int line, unsigned int sense)
548fd931f23SAndrey Smirnov {
549fd931f23SAndrey Smirnov 	/*
550fd931f23SAndrey Smirnov 	 * Every interrupt line is represented by two bits shifted
551fd931f23SAndrey Smirnov 	 * proportionally to the line number
552fd931f23SAndrey Smirnov 	 */
553fd931f23SAndrey Smirnov 	const unsigned int n = line * 2;
554fd931f23SAndrey Smirnov 	const unsigned int mask = ~((SX150X_IRQ_TYPE_EDGE_RISING |
555fd931f23SAndrey Smirnov 				     SX150X_IRQ_TYPE_EDGE_FALLING) << n);
556fd931f23SAndrey Smirnov 
557fd931f23SAndrey Smirnov 	pctl->irq.sense &= mask;
558fd931f23SAndrey Smirnov 	pctl->irq.sense |= sense << n;
559fd931f23SAndrey Smirnov }
560fd931f23SAndrey Smirnov 
5619e80f906SNeil Armstrong static int sx150x_irq_set_type(struct irq_data *d, unsigned int flow_type)
5629e80f906SNeil Armstrong {
5639e80f906SNeil Armstrong 	struct sx150x_pinctrl *pctl =
5649e80f906SNeil Armstrong 			gpiochip_get_data(irq_data_get_irq_chip_data(d));
5659e80f906SNeil Armstrong 	unsigned int n, val = 0;
5669e80f906SNeil Armstrong 
5679e80f906SNeil Armstrong 	if (flow_type & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW))
5689e80f906SNeil Armstrong 		return -EINVAL;
5699e80f906SNeil Armstrong 
5709e80f906SNeil Armstrong 	n = d->hwirq;
5719e80f906SNeil Armstrong 
5729e80f906SNeil Armstrong 	if (flow_type & IRQ_TYPE_EDGE_RISING)
573fd931f23SAndrey Smirnov 		val |= SX150X_IRQ_TYPE_EDGE_RISING;
5749e80f906SNeil Armstrong 	if (flow_type & IRQ_TYPE_EDGE_FALLING)
575fd931f23SAndrey Smirnov 		val |= SX150X_IRQ_TYPE_EDGE_FALLING;
5769e80f906SNeil Armstrong 
577fd931f23SAndrey Smirnov 	sx150x_irq_set_sense(pctl, n, val);
5789e80f906SNeil Armstrong 	return 0;
5799e80f906SNeil Armstrong }
5809e80f906SNeil Armstrong 
5819e80f906SNeil Armstrong static irqreturn_t sx150x_irq_thread_fn(int irq, void *dev_id)
5829e80f906SNeil Armstrong {
5839e80f906SNeil Armstrong 	struct sx150x_pinctrl *pctl = (struct sx150x_pinctrl *)dev_id;
58405a90cc7SAndrey Smirnov 	unsigned long n, status;
5850db0f26cSAndrey Smirnov 	unsigned int val;
58605a90cc7SAndrey Smirnov 	int err;
5879e80f906SNeil Armstrong 
5886489677fSAndrey Smirnov 	err = regmap_read(pctl->regmap, pctl->data->reg_irq_src, &val);
5899e80f906SNeil Armstrong 	if (err < 0)
5906489677fSAndrey Smirnov 		return IRQ_NONE;
5919e80f906SNeil Armstrong 
5926489677fSAndrey Smirnov 	err = regmap_write(pctl->regmap, pctl->data->reg_irq_src, val);
5939e80f906SNeil Armstrong 	if (err < 0)
5946489677fSAndrey Smirnov 		return IRQ_NONE;
5959e80f906SNeil Armstrong 
59605a90cc7SAndrey Smirnov 	status = val;
59705a90cc7SAndrey Smirnov 	for_each_set_bit(n, &status, pctl->data->ngpios)
59805a90cc7SAndrey Smirnov 		handle_nested_irq(irq_find_mapping(pctl->gpio.irqdomain, n));
5999e80f906SNeil Armstrong 
60005a90cc7SAndrey Smirnov 	return IRQ_HANDLED;
6019e80f906SNeil Armstrong }
6029e80f906SNeil Armstrong 
6039e80f906SNeil Armstrong static void sx150x_irq_bus_lock(struct irq_data *d)
6049e80f906SNeil Armstrong {
6059e80f906SNeil Armstrong 	struct sx150x_pinctrl *pctl =
6069e80f906SNeil Armstrong 			gpiochip_get_data(irq_data_get_irq_chip_data(d));
6079e80f906SNeil Armstrong 
6089e80f906SNeil Armstrong 	mutex_lock(&pctl->lock);
6099e80f906SNeil Armstrong }
6109e80f906SNeil Armstrong 
6119e80f906SNeil Armstrong static void sx150x_irq_bus_sync_unlock(struct irq_data *d)
6129e80f906SNeil Armstrong {
6139e80f906SNeil Armstrong 	struct sx150x_pinctrl *pctl =
6149e80f906SNeil Armstrong 			gpiochip_get_data(irq_data_get_irq_chip_data(d));
6159e80f906SNeil Armstrong 
6166489677fSAndrey Smirnov 	regmap_write(pctl->regmap, pctl->data->reg_irq_mask, pctl->irq.masked);
6176489677fSAndrey Smirnov 	regmap_write(pctl->regmap, pctl->data->reg_sense, pctl->irq.sense);
6189e80f906SNeil Armstrong 	mutex_unlock(&pctl->lock);
6199e80f906SNeil Armstrong }
6209e80f906SNeil Armstrong 
6219e80f906SNeil Armstrong static int sx150x_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
6229e80f906SNeil Armstrong 			      unsigned long *config)
6239e80f906SNeil Armstrong {
6249e80f906SNeil Armstrong 	struct sx150x_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
6259e80f906SNeil Armstrong 	unsigned int param = pinconf_to_config_param(*config);
6269e80f906SNeil Armstrong 	int ret;
6279e80f906SNeil Armstrong 	u32 arg;
6280db0f26cSAndrey Smirnov 	unsigned int data;
6299e80f906SNeil Armstrong 
6306489677fSAndrey Smirnov 	if (sx150x_pin_is_oscio(pctl, pin)) {
6319e80f906SNeil Armstrong 		switch (param) {
6329e80f906SNeil Armstrong 		case PIN_CONFIG_DRIVE_PUSH_PULL:
6339e80f906SNeil Armstrong 		case PIN_CONFIG_OUTPUT:
6340db0f26cSAndrey Smirnov 			ret = regmap_read(pctl->regmap,
6359e80f906SNeil Armstrong 					  pctl->data->pri.x789.reg_clock,
6369e80f906SNeil Armstrong 					  &data);
6379e80f906SNeil Armstrong 			if (ret < 0)
6389e80f906SNeil Armstrong 				return ret;
6399e80f906SNeil Armstrong 
6409e80f906SNeil Armstrong 			if (param == PIN_CONFIG_DRIVE_PUSH_PULL)
6419e80f906SNeil Armstrong 				arg = (data & 0x1f) ? 1 : 0;
6429e80f906SNeil Armstrong 			else {
6439e80f906SNeil Armstrong 				if ((data & 0x1f) == 0x1f)
6449e80f906SNeil Armstrong 					arg = 1;
6459e80f906SNeil Armstrong 				else if ((data & 0x1f) == 0x10)
6469e80f906SNeil Armstrong 					arg = 0;
6479e80f906SNeil Armstrong 				else
6489e80f906SNeil Armstrong 					return -EINVAL;
6499e80f906SNeil Armstrong 			}
6509e80f906SNeil Armstrong 
6519e80f906SNeil Armstrong 			break;
6529e80f906SNeil Armstrong 		default:
6539e80f906SNeil Armstrong 			return -ENOTSUPP;
6549e80f906SNeil Armstrong 		}
6559e80f906SNeil Armstrong 
6569e80f906SNeil Armstrong 		goto out;
6579e80f906SNeil Armstrong 	}
6589e80f906SNeil Armstrong 
6599e80f906SNeil Armstrong 	switch (param) {
6609e80f906SNeil Armstrong 	case PIN_CONFIG_BIAS_PULL_DOWN:
6616489677fSAndrey Smirnov 		ret = regmap_read(pctl->regmap,
6626489677fSAndrey Smirnov 				  pctl->data->reg_pulldn,
6636489677fSAndrey Smirnov 				  &data);
6646489677fSAndrey Smirnov 		data &= BIT(pin);
6659e80f906SNeil Armstrong 
6669e80f906SNeil Armstrong 		if (ret < 0)
6679e80f906SNeil Armstrong 			return ret;
6689e80f906SNeil Armstrong 
6699e80f906SNeil Armstrong 		if (!ret)
6709e80f906SNeil Armstrong 			return -EINVAL;
6719e80f906SNeil Armstrong 
6729e80f906SNeil Armstrong 		arg = 1;
6739e80f906SNeil Armstrong 		break;
6749e80f906SNeil Armstrong 
6759e80f906SNeil Armstrong 	case PIN_CONFIG_BIAS_PULL_UP:
6766489677fSAndrey Smirnov 		ret = regmap_read(pctl->regmap,
6776489677fSAndrey Smirnov 				  pctl->data->reg_pullup,
6786489677fSAndrey Smirnov 				  &data);
6796489677fSAndrey Smirnov 		data &= BIT(pin);
6809e80f906SNeil Armstrong 
6819e80f906SNeil Armstrong 		if (ret < 0)
6829e80f906SNeil Armstrong 			return ret;
6839e80f906SNeil Armstrong 
6849e80f906SNeil Armstrong 		if (!ret)
6859e80f906SNeil Armstrong 			return -EINVAL;
6869e80f906SNeil Armstrong 
6879e80f906SNeil Armstrong 		arg = 1;
6889e80f906SNeil Armstrong 		break;
6899e80f906SNeil Armstrong 
6909e80f906SNeil Armstrong 	case PIN_CONFIG_DRIVE_OPEN_DRAIN:
6919e80f906SNeil Armstrong 		if (pctl->data->model != SX150X_789)
6929e80f906SNeil Armstrong 			return -ENOTSUPP;
6939e80f906SNeil Armstrong 
6946489677fSAndrey Smirnov 		ret = regmap_read(pctl->regmap,
6956489677fSAndrey Smirnov 				  pctl->data->pri.x789.reg_drain,
6966489677fSAndrey Smirnov 				  &data);
6976489677fSAndrey Smirnov 		data &= BIT(pin);
6989e80f906SNeil Armstrong 
6999e80f906SNeil Armstrong 		if (ret < 0)
7009e80f906SNeil Armstrong 			return ret;
7019e80f906SNeil Armstrong 
7026489677fSAndrey Smirnov 		if (!data)
7039e80f906SNeil Armstrong 			return -EINVAL;
7049e80f906SNeil Armstrong 
7059e80f906SNeil Armstrong 		arg = 1;
7069e80f906SNeil Armstrong 		break;
7079e80f906SNeil Armstrong 
7089e80f906SNeil Armstrong 	case PIN_CONFIG_DRIVE_PUSH_PULL:
7099e80f906SNeil Armstrong 		if (pctl->data->model != SX150X_789)
7109e80f906SNeil Armstrong 			arg = true;
7119e80f906SNeil Armstrong 		else {
7126489677fSAndrey Smirnov 			ret = regmap_read(pctl->regmap,
7136489677fSAndrey Smirnov 					  pctl->data->pri.x789.reg_drain,
7146489677fSAndrey Smirnov 					  &data);
7156489677fSAndrey Smirnov 			data &= BIT(pin);
7169e80f906SNeil Armstrong 
7179e80f906SNeil Armstrong 			if (ret < 0)
7189e80f906SNeil Armstrong 				return ret;
7199e80f906SNeil Armstrong 
7206489677fSAndrey Smirnov 			if (data)
7219e80f906SNeil Armstrong 				return -EINVAL;
7229e80f906SNeil Armstrong 
7239e80f906SNeil Armstrong 			arg = 1;
7249e80f906SNeil Armstrong 		}
7259e80f906SNeil Armstrong 		break;
7269e80f906SNeil Armstrong 
7279e80f906SNeil Armstrong 	case PIN_CONFIG_OUTPUT:
7289e80f906SNeil Armstrong 		ret = sx150x_gpio_get_direction(&pctl->gpio, pin);
7299e80f906SNeil Armstrong 		if (ret < 0)
7309e80f906SNeil Armstrong 			return ret;
7319e80f906SNeil Armstrong 
7329e80f906SNeil Armstrong 		if (ret)
7339e80f906SNeil Armstrong 			return -EINVAL;
7349e80f906SNeil Armstrong 
7359e80f906SNeil Armstrong 		ret = sx150x_gpio_get(&pctl->gpio, pin);
7369e80f906SNeil Armstrong 		if (ret < 0)
7379e80f906SNeil Armstrong 			return ret;
7389e80f906SNeil Armstrong 
7399e80f906SNeil Armstrong 		arg = ret;
7409e80f906SNeil Armstrong 		break;
7419e80f906SNeil Armstrong 
7429e80f906SNeil Armstrong 	default:
7439e80f906SNeil Armstrong 		return -ENOTSUPP;
7449e80f906SNeil Armstrong 	}
7459e80f906SNeil Armstrong 
7469e80f906SNeil Armstrong out:
7479e80f906SNeil Armstrong 	*config = pinconf_to_config_packed(param, arg);
7489e80f906SNeil Armstrong 
7499e80f906SNeil Armstrong 	return 0;
7509e80f906SNeil Armstrong }
7519e80f906SNeil Armstrong 
7529e80f906SNeil Armstrong static int sx150x_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
7539e80f906SNeil Armstrong 			      unsigned long *configs, unsigned int num_configs)
7549e80f906SNeil Armstrong {
7559e80f906SNeil Armstrong 	struct sx150x_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
7569e80f906SNeil Armstrong 	enum pin_config_param param;
7579e80f906SNeil Armstrong 	u32 arg;
7589e80f906SNeil Armstrong 	int i;
7599e80f906SNeil Armstrong 	int ret;
7609e80f906SNeil Armstrong 
7619e80f906SNeil Armstrong 	for (i = 0; i < num_configs; i++) {
7629e80f906SNeil Armstrong 		param = pinconf_to_config_param(configs[i]);
7639e80f906SNeil Armstrong 		arg = pinconf_to_config_argument(configs[i]);
7649e80f906SNeil Armstrong 
7659e80f906SNeil Armstrong 		if (sx150x_pin_is_oscio(pctl, pin)) {
7669e80f906SNeil Armstrong 			if (param == PIN_CONFIG_OUTPUT) {
7679e80f906SNeil Armstrong 				ret = sx150x_gpio_direction_output(&pctl->gpio,
7689e80f906SNeil Armstrong 								   pin, arg);
7699e80f906SNeil Armstrong 				if (ret < 0)
7709e80f906SNeil Armstrong 					return ret;
7719e80f906SNeil Armstrong 
7729e80f906SNeil Armstrong 				continue;
7739e80f906SNeil Armstrong 			} else
7749e80f906SNeil Armstrong 				return -ENOTSUPP;
7759e80f906SNeil Armstrong 		}
7769e80f906SNeil Armstrong 
7779e80f906SNeil Armstrong 		switch (param) {
7789e80f906SNeil Armstrong 		case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
7799e80f906SNeil Armstrong 		case PIN_CONFIG_BIAS_DISABLE:
7806489677fSAndrey Smirnov 			ret = regmap_write_bits(pctl->regmap,
7816489677fSAndrey Smirnov 						pctl->data->reg_pulldn,
7826489677fSAndrey Smirnov 						BIT(pin), 0);
7839e80f906SNeil Armstrong 			if (ret < 0)
7849e80f906SNeil Armstrong 				return ret;
7859e80f906SNeil Armstrong 
7866489677fSAndrey Smirnov 			ret = regmap_write_bits(pctl->regmap,
7876489677fSAndrey Smirnov 						pctl->data->reg_pullup,
7886489677fSAndrey Smirnov 						BIT(pin), 0);
7899e80f906SNeil Armstrong 			if (ret < 0)
7909e80f906SNeil Armstrong 				return ret;
7919e80f906SNeil Armstrong 
7929e80f906SNeil Armstrong 			break;
7939e80f906SNeil Armstrong 
7949e80f906SNeil Armstrong 		case PIN_CONFIG_BIAS_PULL_UP:
7956489677fSAndrey Smirnov 			ret = regmap_write_bits(pctl->regmap,
7969e80f906SNeil Armstrong 						pctl->data->reg_pullup,
7976489677fSAndrey Smirnov 						BIT(pin), BIT(pin));
7989e80f906SNeil Armstrong 			if (ret < 0)
7999e80f906SNeil Armstrong 				return ret;
8009e80f906SNeil Armstrong 
8019e80f906SNeil Armstrong 			break;
8029e80f906SNeil Armstrong 
8039e80f906SNeil Armstrong 		case PIN_CONFIG_BIAS_PULL_DOWN:
8046489677fSAndrey Smirnov 			ret = regmap_write_bits(pctl->regmap,
8059e80f906SNeil Armstrong 						pctl->data->reg_pulldn,
8066489677fSAndrey Smirnov 						BIT(pin), BIT(pin));
8079e80f906SNeil Armstrong 			if (ret < 0)
8089e80f906SNeil Armstrong 				return ret;
8099e80f906SNeil Armstrong 
8109e80f906SNeil Armstrong 			break;
8119e80f906SNeil Armstrong 
8129e80f906SNeil Armstrong 		case PIN_CONFIG_DRIVE_OPEN_DRAIN:
8139e80f906SNeil Armstrong 			ret = sx150x_gpio_set_single_ended(&pctl->gpio,
8149e80f906SNeil Armstrong 						pin, LINE_MODE_OPEN_DRAIN);
8159e80f906SNeil Armstrong 			if (ret < 0)
8169e80f906SNeil Armstrong 				return ret;
8179e80f906SNeil Armstrong 
8189e80f906SNeil Armstrong 			break;
8199e80f906SNeil Armstrong 
8209e80f906SNeil Armstrong 		case PIN_CONFIG_DRIVE_PUSH_PULL:
8219e80f906SNeil Armstrong 			ret = sx150x_gpio_set_single_ended(&pctl->gpio,
8229e80f906SNeil Armstrong 						pin, LINE_MODE_PUSH_PULL);
8239e80f906SNeil Armstrong 			if (ret < 0)
8249e80f906SNeil Armstrong 				return ret;
8259e80f906SNeil Armstrong 
8269e80f906SNeil Armstrong 			break;
8279e80f906SNeil Armstrong 
8289e80f906SNeil Armstrong 		case PIN_CONFIG_OUTPUT:
8299e80f906SNeil Armstrong 			ret = sx150x_gpio_direction_output(&pctl->gpio,
8309e80f906SNeil Armstrong 							   pin, arg);
8319e80f906SNeil Armstrong 			if (ret < 0)
8329e80f906SNeil Armstrong 				return ret;
8339e80f906SNeil Armstrong 
8349e80f906SNeil Armstrong 			break;
8359e80f906SNeil Armstrong 
8369e80f906SNeil Armstrong 		default:
8379e80f906SNeil Armstrong 			return -ENOTSUPP;
8389e80f906SNeil Armstrong 		}
8399e80f906SNeil Armstrong 	} /* for each config */
8409e80f906SNeil Armstrong 
8419e80f906SNeil Armstrong 	return 0;
8429e80f906SNeil Armstrong }
8439e80f906SNeil Armstrong 
8449e80f906SNeil Armstrong static const struct pinconf_ops sx150x_pinconf_ops = {
8459e80f906SNeil Armstrong 	.pin_config_get = sx150x_pinconf_get,
8469e80f906SNeil Armstrong 	.pin_config_set = sx150x_pinconf_set,
8479e80f906SNeil Armstrong 	.is_generic = true,
8489e80f906SNeil Armstrong };
8499e80f906SNeil Armstrong 
8509e80f906SNeil Armstrong static const struct i2c_device_id sx150x_id[] = {
851*4f5ac8cfSPeter Rosin 	{"sx1501q", (kernel_ulong_t) &sx1501q_device_data },
8529e80f906SNeil Armstrong 	{"sx1502q", (kernel_ulong_t) &sx1502q_device_data },
8536697546dSAndrey Smirnov 	{"sx1503q", (kernel_ulong_t) &sx1503q_device_data },
854*4f5ac8cfSPeter Rosin 	{"sx1504q", (kernel_ulong_t) &sx1504q_device_data },
855*4f5ac8cfSPeter Rosin 	{"sx1505q", (kernel_ulong_t) &sx1505q_device_data },
856bba709bdSPeter Rosin 	{"sx1506q", (kernel_ulong_t) &sx1506q_device_data },
857*4f5ac8cfSPeter Rosin 	{"sx1507q", (kernel_ulong_t) &sx1507q_device_data },
858bba709bdSPeter Rosin 	{"sx1508q", (kernel_ulong_t) &sx1508q_device_data },
859bba709bdSPeter Rosin 	{"sx1509q", (kernel_ulong_t) &sx1509q_device_data },
8609e80f906SNeil Armstrong 	{}
8619e80f906SNeil Armstrong };
8629e80f906SNeil Armstrong 
8639e80f906SNeil Armstrong static const struct of_device_id sx150x_of_match[] = {
864*4f5ac8cfSPeter Rosin 	{ .compatible = "semtech,sx1501q", .data = &sx1501q_device_data },
865e3ba8120SAndrey Smirnov 	{ .compatible = "semtech,sx1502q", .data = &sx1502q_device_data },
8666697546dSAndrey Smirnov 	{ .compatible = "semtech,sx1503q", .data = &sx1503q_device_data },
867*4f5ac8cfSPeter Rosin 	{ .compatible = "semtech,sx1504q", .data = &sx1504q_device_data },
868*4f5ac8cfSPeter Rosin 	{ .compatible = "semtech,sx1505q", .data = &sx1505q_device_data },
869bba709bdSPeter Rosin 	{ .compatible = "semtech,sx1506q", .data = &sx1506q_device_data },
870*4f5ac8cfSPeter Rosin 	{ .compatible = "semtech,sx1507q", .data = &sx1507q_device_data },
871bba709bdSPeter Rosin 	{ .compatible = "semtech,sx1508q", .data = &sx1508q_device_data },
872bba709bdSPeter Rosin 	{ .compatible = "semtech,sx1509q", .data = &sx1509q_device_data },
8739e80f906SNeil Armstrong 	{},
8749e80f906SNeil Armstrong };
8759e80f906SNeil Armstrong 
8769e80f906SNeil Armstrong static int sx150x_reset(struct sx150x_pinctrl *pctl)
8779e80f906SNeil Armstrong {
8789e80f906SNeil Armstrong 	int err;
8799e80f906SNeil Armstrong 
8809e80f906SNeil Armstrong 	err = i2c_smbus_write_byte_data(pctl->client,
8819e80f906SNeil Armstrong 					pctl->data->pri.x789.reg_reset,
882f9038d60SAndrey Smirnov 					SX150X_789_RESET_KEY1);
8839e80f906SNeil Armstrong 	if (err < 0)
8849e80f906SNeil Armstrong 		return err;
8859e80f906SNeil Armstrong 
8869e80f906SNeil Armstrong 	err = i2c_smbus_write_byte_data(pctl->client,
8879e80f906SNeil Armstrong 					pctl->data->pri.x789.reg_reset,
888f9038d60SAndrey Smirnov 					SX150X_789_RESET_KEY2);
8899e80f906SNeil Armstrong 	return err;
8909e80f906SNeil Armstrong }
8919e80f906SNeil Armstrong 
892310cdfa0SAndrey Smirnov static int sx150x_init_misc(struct sx150x_pinctrl *pctl)
893310cdfa0SAndrey Smirnov {
894310cdfa0SAndrey Smirnov 	u8 reg, value;
895310cdfa0SAndrey Smirnov 
896310cdfa0SAndrey Smirnov 	switch (pctl->data->model) {
897310cdfa0SAndrey Smirnov 	case SX150X_789:
898310cdfa0SAndrey Smirnov 		reg   = pctl->data->pri.x789.reg_misc;
899310cdfa0SAndrey Smirnov 		value = SX150X_789_REG_MISC_AUTOCLEAR_OFF;
900310cdfa0SAndrey Smirnov 		break;
901310cdfa0SAndrey Smirnov 	case SX150X_456:
902310cdfa0SAndrey Smirnov 		reg   = pctl->data->pri.x456.reg_advance;
903310cdfa0SAndrey Smirnov 		value = 0x00;
904b30d31e4SAndrey Smirnov 
905b30d31e4SAndrey Smirnov 		/*
906b30d31e4SAndrey Smirnov 		 * Only SX1506 has RegAdvanced, SX1504/5 are expected
907b30d31e4SAndrey Smirnov 		 * to initialize this offset to zero
908b30d31e4SAndrey Smirnov 		 */
909b30d31e4SAndrey Smirnov 		if (!reg)
910b30d31e4SAndrey Smirnov 			return 0;
911310cdfa0SAndrey Smirnov 		break;
912310cdfa0SAndrey Smirnov 	case SX150X_123:
913310cdfa0SAndrey Smirnov 		reg   = pctl->data->pri.x123.reg_advance;
914310cdfa0SAndrey Smirnov 		value = 0x00;
915310cdfa0SAndrey Smirnov 		break;
916310cdfa0SAndrey Smirnov 	default:
917310cdfa0SAndrey Smirnov 		WARN(1, "Unknown chip model %d\n", pctl->data->model);
918310cdfa0SAndrey Smirnov 		return -EINVAL;
919310cdfa0SAndrey Smirnov 	}
920310cdfa0SAndrey Smirnov 
9216489677fSAndrey Smirnov 	return regmap_write(pctl->regmap, reg, value);
922310cdfa0SAndrey Smirnov }
923310cdfa0SAndrey Smirnov 
9249e80f906SNeil Armstrong static int sx150x_init_hw(struct sx150x_pinctrl *pctl)
9259e80f906SNeil Armstrong {
9266489677fSAndrey Smirnov 	const u8 reg[] = {
9276489677fSAndrey Smirnov 		[SX150X_789] = pctl->data->pri.x789.reg_polarity,
9286489677fSAndrey Smirnov 		[SX150X_456] = pctl->data->pri.x456.reg_pld_mode,
9296489677fSAndrey Smirnov 		[SX150X_123] = pctl->data->pri.x123.reg_pld_mode,
9306489677fSAndrey Smirnov 	};
9319e80f906SNeil Armstrong 	int err;
9329e80f906SNeil Armstrong 
9339e80f906SNeil Armstrong 	if (pctl->data->model == SX150X_789 &&
9349e80f906SNeil Armstrong 	    of_property_read_bool(pctl->dev->of_node, "semtech,probe-reset")) {
9359e80f906SNeil Armstrong 		err = sx150x_reset(pctl);
9369e80f906SNeil Armstrong 		if (err < 0)
9379e80f906SNeil Armstrong 			return err;
9389e80f906SNeil Armstrong 	}
9399e80f906SNeil Armstrong 
940310cdfa0SAndrey Smirnov 	err = sx150x_init_misc(pctl);
9419e80f906SNeil Armstrong 	if (err < 0)
9429e80f906SNeil Armstrong 		return err;
9439e80f906SNeil Armstrong 
9449e80f906SNeil Armstrong 	/* Set all pins to work in normal mode */
9456489677fSAndrey Smirnov 	return regmap_write(pctl->regmap, reg[pctl->data->model], 0);
9469e80f906SNeil Armstrong }
9479e80f906SNeil Armstrong 
9486489677fSAndrey Smirnov static int sx150x_regmap_reg_width(struct sx150x_pinctrl *pctl,
9496489677fSAndrey Smirnov 				   unsigned int reg)
9506489677fSAndrey Smirnov {
9516489677fSAndrey Smirnov 	const struct sx150x_device_data *data = pctl->data;
9526489677fSAndrey Smirnov 
9536489677fSAndrey Smirnov 	if (reg == data->reg_sense) {
9546489677fSAndrey Smirnov 		/*
9556489677fSAndrey Smirnov 		 * RegSense packs two bits of configuration per GPIO,
9566489677fSAndrey Smirnov 		 * so we'd need to read twice as many bits as there
9576489677fSAndrey Smirnov 		 * are GPIO in our chip
9586489677fSAndrey Smirnov 		 */
9596489677fSAndrey Smirnov 		return 2 * data->ngpios;
9606489677fSAndrey Smirnov 	} else if ((data->model == SX150X_789 &&
9616489677fSAndrey Smirnov 		    (reg == data->pri.x789.reg_misc ||
9626489677fSAndrey Smirnov 		     reg == data->pri.x789.reg_clock ||
9636489677fSAndrey Smirnov 		     reg == data->pri.x789.reg_reset))
9646489677fSAndrey Smirnov 		   ||
9656489677fSAndrey Smirnov 		   (data->model == SX150X_123 &&
9666489677fSAndrey Smirnov 		    reg == data->pri.x123.reg_advance)
9676489677fSAndrey Smirnov 		   ||
9686489677fSAndrey Smirnov 		   (data->model == SX150X_456 &&
9696489677fSAndrey Smirnov 		    reg == data->pri.x456.reg_advance)) {
9706489677fSAndrey Smirnov 		return 8;
9716489677fSAndrey Smirnov 	} else {
9726489677fSAndrey Smirnov 		return data->ngpios;
9736489677fSAndrey Smirnov 	}
9746489677fSAndrey Smirnov }
9756489677fSAndrey Smirnov 
9766489677fSAndrey Smirnov static unsigned int sx150x_maybe_swizzle(struct sx150x_pinctrl *pctl,
9776489677fSAndrey Smirnov 					 unsigned int reg, unsigned int val)
9786489677fSAndrey Smirnov {
9796489677fSAndrey Smirnov 	unsigned int a, b;
9806489677fSAndrey Smirnov 	const struct sx150x_device_data *data = pctl->data;
9816489677fSAndrey Smirnov 
9826489677fSAndrey Smirnov 	/*
9836489677fSAndrey Smirnov 	 * Whereas SX1509 presents RegSense in a simple layout as such:
9846489677fSAndrey Smirnov 	 *	reg     [ f f e e d d c c ]
9856489677fSAndrey Smirnov 	 *	reg + 1 [ b b a a 9 9 8 8 ]
9866489677fSAndrey Smirnov 	 *	reg + 2 [ 7 7 6 6 5 5 4 4 ]
9876489677fSAndrey Smirnov 	 *	reg + 3 [ 3 3 2 2 1 1 0 0 ]
9886489677fSAndrey Smirnov 	 *
9896489677fSAndrey Smirnov 	 * SX1503 and SX1506 deviate from that data layout, instead storing
9907bd47496SPeter Rosin 	 * their contents as follows:
9916489677fSAndrey Smirnov 	 *
9926489677fSAndrey Smirnov 	 *	reg     [ f f e e d d c c ]
9936489677fSAndrey Smirnov 	 *	reg + 1 [ 7 7 6 6 5 5 4 4 ]
9946489677fSAndrey Smirnov 	 *	reg + 2 [ b b a a 9 9 8 8 ]
9956489677fSAndrey Smirnov 	 *	reg + 3 [ 3 3 2 2 1 1 0 0 ]
9966489677fSAndrey Smirnov 	 *
9976489677fSAndrey Smirnov 	 * so, taking that into account, we swap two
9986489677fSAndrey Smirnov 	 * inner bytes of a 4-byte result
9996489677fSAndrey Smirnov 	 */
10006489677fSAndrey Smirnov 
10016489677fSAndrey Smirnov 	if (reg == data->reg_sense &&
10026489677fSAndrey Smirnov 	    data->ngpios == 16 &&
10036489677fSAndrey Smirnov 	    (data->model == SX150X_123 ||
10046489677fSAndrey Smirnov 	     data->model == SX150X_456)) {
10056489677fSAndrey Smirnov 		a = val & 0x00ff0000;
10066489677fSAndrey Smirnov 		b = val & 0x0000ff00;
10076489677fSAndrey Smirnov 
10086489677fSAndrey Smirnov 		val &= 0xff0000ff;
10096489677fSAndrey Smirnov 		val |= b << 8;
10106489677fSAndrey Smirnov 		val |= a >> 8;
10116489677fSAndrey Smirnov 	}
10126489677fSAndrey Smirnov 
10136489677fSAndrey Smirnov 	return val;
10146489677fSAndrey Smirnov }
10156489677fSAndrey Smirnov 
10166489677fSAndrey Smirnov /*
10176489677fSAndrey Smirnov  * In order to mask the differences between 16 and 8 bit expander
10186489677fSAndrey Smirnov  * devices we set up a sligthly ficticious regmap that pretends to be
10196489677fSAndrey Smirnov  * a set of 32-bit (to accomodate RegSenseLow/RegSenseHigh
10206489677fSAndrey Smirnov  * pair/quartet) registers and transparently reconstructs those
10216489677fSAndrey Smirnov  * registers via multiple I2C/SMBus reads
10226489677fSAndrey Smirnov  *
10236489677fSAndrey Smirnov  * This way the rest of the driver code, interfacing with the chip via
10246489677fSAndrey Smirnov  * regmap API, can work assuming that each GPIO pin is represented by
10257bd47496SPeter Rosin  * a group of bits at an offset proportional to GPIO number within a
10266489677fSAndrey Smirnov  * given register.
10276489677fSAndrey Smirnov  */
10286489677fSAndrey Smirnov static int sx150x_regmap_reg_read(void *context, unsigned int reg,
10296489677fSAndrey Smirnov 				  unsigned int *result)
10306489677fSAndrey Smirnov {
10316489677fSAndrey Smirnov 	int ret, n;
10326489677fSAndrey Smirnov 	struct sx150x_pinctrl *pctl = context;
10336489677fSAndrey Smirnov 	struct i2c_client *i2c = pctl->client;
10346489677fSAndrey Smirnov 	const int width = sx150x_regmap_reg_width(pctl, reg);
10356489677fSAndrey Smirnov 	unsigned int idx, val;
10366489677fSAndrey Smirnov 
10376489677fSAndrey Smirnov 	/*
10387bd47496SPeter Rosin 	 * There are four potential cases covered by this function:
10396489677fSAndrey Smirnov 	 *
10406489677fSAndrey Smirnov 	 * 1) 8-pin chip, single configuration bit register
10416489677fSAndrey Smirnov 	 *
10426489677fSAndrey Smirnov 	 *	This is trivial the code below just needs to read:
10436489677fSAndrey Smirnov 	 *		reg  [ 7 6 5 4 3 2 1 0 ]
10446489677fSAndrey Smirnov 	 *
10456489677fSAndrey Smirnov 	 * 2) 8-pin chip, double configuration bit register (RegSense)
10466489677fSAndrey Smirnov 	 *
10476489677fSAndrey Smirnov 	 *	The read will be done as follows:
10486489677fSAndrey Smirnov 	 *		reg      [ 7 7 6 6 5 5 4 4 ]
10496489677fSAndrey Smirnov 	 *		reg + 1  [ 3 3 2 2 1 1 0 0 ]
10506489677fSAndrey Smirnov 	 *
10516489677fSAndrey Smirnov 	 * 3) 16-pin chip, single configuration bit register
10526489677fSAndrey Smirnov 	 *
10536489677fSAndrey Smirnov 	 *	The read will be done as follows:
10546489677fSAndrey Smirnov 	 *		reg     [ f e d c b a 9 8 ]
10556489677fSAndrey Smirnov 	 *		reg + 1 [ 7 6 5 4 3 2 1 0 ]
10566489677fSAndrey Smirnov 	 *
10576489677fSAndrey Smirnov 	 * 4) 16-pin chip, double configuration bit register (RegSense)
10586489677fSAndrey Smirnov 	 *
10596489677fSAndrey Smirnov 	 *	The read will be done as follows:
10606489677fSAndrey Smirnov 	 *		reg     [ f f e e d d c c ]
10616489677fSAndrey Smirnov 	 *		reg + 1 [ b b a a 9 9 8 8 ]
10626489677fSAndrey Smirnov 	 *		reg + 2 [ 7 7 6 6 5 5 4 4 ]
10636489677fSAndrey Smirnov 	 *		reg + 3 [ 3 3 2 2 1 1 0 0 ]
10646489677fSAndrey Smirnov 	 */
10656489677fSAndrey Smirnov 
10666489677fSAndrey Smirnov 	for (n = width, val = 0, idx = reg; n > 0; n -= 8, idx++) {
10676489677fSAndrey Smirnov 		val <<= 8;
10686489677fSAndrey Smirnov 
10696489677fSAndrey Smirnov 		ret = i2c_smbus_read_byte_data(i2c, idx);
10706489677fSAndrey Smirnov 		if (ret < 0)
10716489677fSAndrey Smirnov 			return ret;
10726489677fSAndrey Smirnov 
10736489677fSAndrey Smirnov 		val |= ret;
10746489677fSAndrey Smirnov 	}
10756489677fSAndrey Smirnov 
10766489677fSAndrey Smirnov 	*result = sx150x_maybe_swizzle(pctl, reg, val);
10776489677fSAndrey Smirnov 
10786489677fSAndrey Smirnov 	return 0;
10796489677fSAndrey Smirnov }
10806489677fSAndrey Smirnov 
10816489677fSAndrey Smirnov static int sx150x_regmap_reg_write(void *context, unsigned int reg,
10826489677fSAndrey Smirnov 				   unsigned int val)
10836489677fSAndrey Smirnov {
10846489677fSAndrey Smirnov 	int ret, n;
10856489677fSAndrey Smirnov 	struct sx150x_pinctrl *pctl = context;
10866489677fSAndrey Smirnov 	struct i2c_client *i2c = pctl->client;
10876489677fSAndrey Smirnov 	const int width = sx150x_regmap_reg_width(pctl, reg);
10886489677fSAndrey Smirnov 
10896489677fSAndrey Smirnov 	val = sx150x_maybe_swizzle(pctl, reg, val);
10906489677fSAndrey Smirnov 
10916489677fSAndrey Smirnov 	n = width - 8;
10926489677fSAndrey Smirnov 	do {
10936489677fSAndrey Smirnov 		const u8 byte = (val >> n) & 0xff;
10946489677fSAndrey Smirnov 
10956489677fSAndrey Smirnov 		ret = i2c_smbus_write_byte_data(i2c, reg, byte);
10966489677fSAndrey Smirnov 		if (ret < 0)
10976489677fSAndrey Smirnov 			return ret;
10986489677fSAndrey Smirnov 
10996489677fSAndrey Smirnov 		reg++;
11006489677fSAndrey Smirnov 		n -= 8;
11016489677fSAndrey Smirnov 	} while (n >= 0);
11026489677fSAndrey Smirnov 
11039e80f906SNeil Armstrong 	return 0;
11049e80f906SNeil Armstrong }
11059e80f906SNeil Armstrong 
11060db0f26cSAndrey Smirnov static bool sx150x_reg_volatile(struct device *dev, unsigned int reg)
11070db0f26cSAndrey Smirnov {
11080db0f26cSAndrey Smirnov 	struct sx150x_pinctrl *pctl = i2c_get_clientdata(to_i2c_client(dev));
11090db0f26cSAndrey Smirnov 
11106489677fSAndrey Smirnov 	return reg == pctl->data->reg_irq_src || reg == pctl->data->reg_data;
11110db0f26cSAndrey Smirnov }
11120db0f26cSAndrey Smirnov 
11130db0f26cSAndrey Smirnov const struct regmap_config sx150x_regmap_config = {
11140db0f26cSAndrey Smirnov 	.reg_bits = 8,
11156489677fSAndrey Smirnov 	.val_bits = 32,
11160db0f26cSAndrey Smirnov 
11170db0f26cSAndrey Smirnov 	.cache_type = REGCACHE_RBTREE,
11180db0f26cSAndrey Smirnov 
11196489677fSAndrey Smirnov 	.reg_read = sx150x_regmap_reg_read,
11206489677fSAndrey Smirnov 	.reg_write = sx150x_regmap_reg_write,
11216489677fSAndrey Smirnov 
11220db0f26cSAndrey Smirnov 	.max_register = SX150X_MAX_REGISTER,
11230db0f26cSAndrey Smirnov 	.volatile_reg = sx150x_reg_volatile,
11240db0f26cSAndrey Smirnov };
11250db0f26cSAndrey Smirnov 
11269e80f906SNeil Armstrong static int sx150x_probe(struct i2c_client *client,
11279e80f906SNeil Armstrong 			const struct i2c_device_id *id)
11289e80f906SNeil Armstrong {
11299e80f906SNeil Armstrong 	static const u32 i2c_funcs = I2C_FUNC_SMBUS_BYTE_DATA |
11309e80f906SNeil Armstrong 				     I2C_FUNC_SMBUS_WRITE_WORD_DATA;
11319e80f906SNeil Armstrong 	struct device *dev = &client->dev;
11329e80f906SNeil Armstrong 	struct sx150x_pinctrl *pctl;
11339e80f906SNeil Armstrong 	int ret;
11349e80f906SNeil Armstrong 
11359e80f906SNeil Armstrong 	if (!i2c_check_functionality(client->adapter, i2c_funcs))
11369e80f906SNeil Armstrong 		return -ENOSYS;
11379e80f906SNeil Armstrong 
11389e80f906SNeil Armstrong 	pctl = devm_kzalloc(dev, sizeof(*pctl), GFP_KERNEL);
11399e80f906SNeil Armstrong 	if (!pctl)
11409e80f906SNeil Armstrong 		return -ENOMEM;
11419e80f906SNeil Armstrong 
11420db0f26cSAndrey Smirnov 	i2c_set_clientdata(client, pctl);
11430db0f26cSAndrey Smirnov 
11449e80f906SNeil Armstrong 	pctl->dev = dev;
11459e80f906SNeil Armstrong 	pctl->client = client;
1146e3ba8120SAndrey Smirnov 
1147e3ba8120SAndrey Smirnov 	if (dev->of_node)
1148e3ba8120SAndrey Smirnov 		pctl->data = of_device_get_match_data(dev);
1149e3ba8120SAndrey Smirnov 	else
1150e3ba8120SAndrey Smirnov 		pctl->data = (struct sx150x_device_data *)id->driver_data;
1151e3ba8120SAndrey Smirnov 
1152e3ba8120SAndrey Smirnov 	if (!pctl->data)
1153e3ba8120SAndrey Smirnov 		return -EINVAL;
11549e80f906SNeil Armstrong 
11556489677fSAndrey Smirnov 	pctl->regmap = devm_regmap_init(dev, NULL, pctl,
11566489677fSAndrey Smirnov 					&sx150x_regmap_config);
11570db0f26cSAndrey Smirnov 	if (IS_ERR(pctl->regmap)) {
11580db0f26cSAndrey Smirnov 		ret = PTR_ERR(pctl->regmap);
11590db0f26cSAndrey Smirnov 		dev_err(dev, "Failed to allocate register map: %d\n",
11600db0f26cSAndrey Smirnov 			ret);
11610db0f26cSAndrey Smirnov 		return ret;
11620db0f26cSAndrey Smirnov 	}
11630db0f26cSAndrey Smirnov 
11649e80f906SNeil Armstrong 	mutex_init(&pctl->lock);
11659e80f906SNeil Armstrong 
11669e80f906SNeil Armstrong 	ret = sx150x_init_hw(pctl);
11679e80f906SNeil Armstrong 	if (ret)
11689e80f906SNeil Armstrong 		return ret;
11699e80f906SNeil Armstrong 
11709e80f906SNeil Armstrong 	/* Register GPIO controller */
11719e80f906SNeil Armstrong 	pctl->gpio.label = devm_kstrdup(dev, client->name, GFP_KERNEL);
11729e80f906SNeil Armstrong 	pctl->gpio.base = -1;
11739e80f906SNeil Armstrong 	pctl->gpio.ngpio = pctl->data->npins;
11749e80f906SNeil Armstrong 	pctl->gpio.get_direction = sx150x_gpio_get_direction;
11759e80f906SNeil Armstrong 	pctl->gpio.direction_input = sx150x_gpio_direction_input;
11769e80f906SNeil Armstrong 	pctl->gpio.direction_output = sx150x_gpio_direction_output;
11779e80f906SNeil Armstrong 	pctl->gpio.get = sx150x_gpio_get;
11789e80f906SNeil Armstrong 	pctl->gpio.set = sx150x_gpio_set;
11799e80f906SNeil Armstrong 	pctl->gpio.set_single_ended = sx150x_gpio_set_single_ended;
11809e80f906SNeil Armstrong 	pctl->gpio.parent = dev;
11819e80f906SNeil Armstrong #ifdef CONFIG_OF_GPIO
11829e80f906SNeil Armstrong 	pctl->gpio.of_node = dev->of_node;
11839e80f906SNeil Armstrong #endif
11849e80f906SNeil Armstrong 	pctl->gpio.can_sleep = true;
1185ec61168bSPeter Rosin 	/*
1186ec61168bSPeter Rosin 	 * Setting multiple pins is not safe when all pins are not
1187ec61168bSPeter Rosin 	 * handled by the same regmap register. The oscio pin (present
1188ec61168bSPeter Rosin 	 * on the SX150X_789 chips) lives in its own register, so
1189ec61168bSPeter Rosin 	 * would require locking that is not in place at this time.
1190ec61168bSPeter Rosin 	 */
1191ec61168bSPeter Rosin 	if (pctl->data->model != SX150X_789)
1192ec61168bSPeter Rosin 		pctl->gpio.set_multiple = sx150x_gpio_set_multiple;
11939e80f906SNeil Armstrong 
11949e80f906SNeil Armstrong 	ret = devm_gpiochip_add_data(dev, &pctl->gpio, pctl);
11959e80f906SNeil Armstrong 	if (ret)
11969e80f906SNeil Armstrong 		return ret;
11979e80f906SNeil Armstrong 
11989e80f906SNeil Armstrong 	/* Add Interrupt support if an irq is specified */
11999e80f906SNeil Armstrong 	if (client->irq > 0) {
12009e80f906SNeil Armstrong 		pctl->irq_chip.name = devm_kstrdup(dev, client->name,
12019e80f906SNeil Armstrong 						   GFP_KERNEL);
12029e80f906SNeil Armstrong 		pctl->irq_chip.irq_mask = sx150x_irq_mask;
12039e80f906SNeil Armstrong 		pctl->irq_chip.irq_unmask = sx150x_irq_unmask;
12049e80f906SNeil Armstrong 		pctl->irq_chip.irq_set_type = sx150x_irq_set_type;
12059e80f906SNeil Armstrong 		pctl->irq_chip.irq_bus_lock = sx150x_irq_bus_lock;
12069e80f906SNeil Armstrong 		pctl->irq_chip.irq_bus_sync_unlock = sx150x_irq_bus_sync_unlock;
12079e80f906SNeil Armstrong 
12089e80f906SNeil Armstrong 		pctl->irq.masked = ~0;
12099e80f906SNeil Armstrong 		pctl->irq.sense = 0;
12109e80f906SNeil Armstrong 
1211080c489dSAndrey Smirnov 		/*
1212080c489dSAndrey Smirnov 		 * Because sx150x_irq_threaded_fn invokes all of the
1213080c489dSAndrey Smirnov 		 * nested interrrupt handlers via handle_nested_irq,
1214080c489dSAndrey Smirnov 		 * any "handler" passed to gpiochip_irqchip_add()
1215080c489dSAndrey Smirnov 		 * below is going to be ignored, so the choice of the
1216080c489dSAndrey Smirnov 		 * function does not matter that much.
1217080c489dSAndrey Smirnov 		 *
1218080c489dSAndrey Smirnov 		 * We set it to handle_bad_irq to avoid confusion,
1219080c489dSAndrey Smirnov 		 * plus it will be instantly noticeable if it is ever
1220080c489dSAndrey Smirnov 		 * called (should not happen)
1221080c489dSAndrey Smirnov 		 */
12229e80f906SNeil Armstrong 		ret = gpiochip_irqchip_add(&pctl->gpio,
12239e80f906SNeil Armstrong 					   &pctl->irq_chip, 0,
1224080c489dSAndrey Smirnov 					   handle_bad_irq, IRQ_TYPE_NONE);
12259e80f906SNeil Armstrong 		if (ret) {
12269e80f906SNeil Armstrong 			dev_err(dev, "could not connect irqchip to gpiochip\n");
12279e80f906SNeil Armstrong 			return ret;
12289e80f906SNeil Armstrong 		}
12299e80f906SNeil Armstrong 
12309e80f906SNeil Armstrong 		ret = devm_request_threaded_irq(dev, client->irq, NULL,
12319e80f906SNeil Armstrong 						sx150x_irq_thread_fn,
12329e80f906SNeil Armstrong 						IRQF_ONESHOT | IRQF_SHARED |
12339e80f906SNeil Armstrong 						IRQF_TRIGGER_FALLING,
12349e80f906SNeil Armstrong 						pctl->irq_chip.name, pctl);
12359e80f906SNeil Armstrong 		if (ret < 0)
12369e80f906SNeil Armstrong 			return ret;
12379e80f906SNeil Armstrong 	}
12389e80f906SNeil Armstrong 
12399e80f906SNeil Armstrong 	/* Pinctrl_desc */
12409e80f906SNeil Armstrong 	pctl->pinctrl_desc.name = "sx150x-pinctrl";
12419e80f906SNeil Armstrong 	pctl->pinctrl_desc.pctlops = &sx150x_pinctrl_ops;
12429e80f906SNeil Armstrong 	pctl->pinctrl_desc.confops = &sx150x_pinconf_ops;
12439e80f906SNeil Armstrong 	pctl->pinctrl_desc.pins = pctl->data->pins;
12449e80f906SNeil Armstrong 	pctl->pinctrl_desc.npins = pctl->data->npins;
12459e80f906SNeil Armstrong 	pctl->pinctrl_desc.owner = THIS_MODULE;
12469e80f906SNeil Armstrong 
12479e80f906SNeil Armstrong 	pctl->pctldev = pinctrl_register(&pctl->pinctrl_desc, dev, pctl);
12489e80f906SNeil Armstrong 	if (IS_ERR(pctl->pctldev)) {
12499e80f906SNeil Armstrong 		dev_err(dev, "Failed to register pinctrl device\n");
12509e80f906SNeil Armstrong 		return PTR_ERR(pctl->pctldev);
12519e80f906SNeil Armstrong 	}
12529e80f906SNeil Armstrong 
12539e80f906SNeil Armstrong 	return 0;
12549e80f906SNeil Armstrong }
12559e80f906SNeil Armstrong 
12569e80f906SNeil Armstrong static struct i2c_driver sx150x_driver = {
12579e80f906SNeil Armstrong 	.driver = {
12589e80f906SNeil Armstrong 		.name = "sx150x-pinctrl",
12599e80f906SNeil Armstrong 		.of_match_table = of_match_ptr(sx150x_of_match),
12609e80f906SNeil Armstrong 	},
12619e80f906SNeil Armstrong 	.probe    = sx150x_probe,
12629e80f906SNeil Armstrong 	.id_table = sx150x_id,
12639e80f906SNeil Armstrong };
12649e80f906SNeil Armstrong 
12659e80f906SNeil Armstrong static int __init sx150x_init(void)
12669e80f906SNeil Armstrong {
12679e80f906SNeil Armstrong 	return i2c_add_driver(&sx150x_driver);
12689e80f906SNeil Armstrong }
12699e80f906SNeil Armstrong subsys_initcall(sx150x_init);
1270