197fb5e8dSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
29e80f906SNeil Armstrong /*
39e80f906SNeil Armstrong * Copyright (c) 2016, BayLibre, SAS. All rights reserved.
49e80f906SNeil Armstrong * Author: Neil Armstrong <narmstrong@baylibre.com>
59e80f906SNeil Armstrong *
69e80f906SNeil Armstrong * Copyright (c) 2010, Code Aurora Forum. All rights reserved.
79e80f906SNeil Armstrong *
89e80f906SNeil Armstrong * Driver for Semtech SX150X I2C GPIO Expanders
96c4ef627SPeter Rosin * The handling of the 4-bit chips (SX1501/SX1504/SX1507) is untested.
109e80f906SNeil Armstrong *
119e80f906SNeil Armstrong * Author: Gregory Bean <gbean@codeaurora.org>
129e80f906SNeil Armstrong */
139e80f906SNeil Armstrong
140db0f26cSAndrey Smirnov #include <linux/regmap.h>
159e80f906SNeil Armstrong #include <linux/i2c.h>
169e80f906SNeil Armstrong #include <linux/init.h>
179e80f906SNeil Armstrong #include <linux/interrupt.h>
189e80f906SNeil Armstrong #include <linux/irq.h>
199e80f906SNeil Armstrong #include <linux/mutex.h>
209e80f906SNeil Armstrong #include <linux/slab.h>
219e80f906SNeil Armstrong #include <linux/of.h>
22fc669d13SLinus Walleij #include <linux/gpio/driver.h>
239e80f906SNeil Armstrong #include <linux/pinctrl/pinconf.h>
249e80f906SNeil Armstrong #include <linux/pinctrl/pinctrl.h>
259e80f906SNeil Armstrong #include <linux/pinctrl/pinmux.h>
269e80f906SNeil Armstrong #include <linux/pinctrl/pinconf-generic.h>
279e80f906SNeil Armstrong
289e80f906SNeil Armstrong #include "core.h"
299e80f906SNeil Armstrong #include "pinconf.h"
309e80f906SNeil Armstrong #include "pinctrl-utils.h"
319e80f906SNeil Armstrong
329e80f906SNeil Armstrong /* The chip models of sx150x */
339e80f906SNeil Armstrong enum {
349e80f906SNeil Armstrong SX150X_123 = 0,
359e80f906SNeil Armstrong SX150X_456,
369e80f906SNeil Armstrong SX150X_789,
379e80f906SNeil Armstrong };
387d68a79aSAndrey Smirnov enum {
397d68a79aSAndrey Smirnov SX150X_789_REG_MISC_AUTOCLEAR_OFF = 1 << 0,
400db0f26cSAndrey Smirnov SX150X_MAX_REGISTER = 0xad,
41fd931f23SAndrey Smirnov SX150X_IRQ_TYPE_EDGE_RISING = 0x1,
42fd931f23SAndrey Smirnov SX150X_IRQ_TYPE_EDGE_FALLING = 0x2,
43f9038d60SAndrey Smirnov SX150X_789_RESET_KEY1 = 0x12,
44f9038d60SAndrey Smirnov SX150X_789_RESET_KEY2 = 0x34,
457d68a79aSAndrey Smirnov };
469e80f906SNeil Armstrong
479e80f906SNeil Armstrong struct sx150x_123_pri {
489e80f906SNeil Armstrong u8 reg_pld_mode;
499e80f906SNeil Armstrong u8 reg_pld_table0;
509e80f906SNeil Armstrong u8 reg_pld_table1;
519e80f906SNeil Armstrong u8 reg_pld_table2;
529e80f906SNeil Armstrong u8 reg_pld_table3;
539e80f906SNeil Armstrong u8 reg_pld_table4;
54c9d26f1aSPeter Rosin u8 reg_advanced;
559e80f906SNeil Armstrong };
569e80f906SNeil Armstrong
579e80f906SNeil Armstrong struct sx150x_456_pri {
589e80f906SNeil Armstrong u8 reg_pld_mode;
599e80f906SNeil Armstrong u8 reg_pld_table0;
609e80f906SNeil Armstrong u8 reg_pld_table1;
619e80f906SNeil Armstrong u8 reg_pld_table2;
629e80f906SNeil Armstrong u8 reg_pld_table3;
639e80f906SNeil Armstrong u8 reg_pld_table4;
64c9d26f1aSPeter Rosin u8 reg_advanced;
659e80f906SNeil Armstrong };
669e80f906SNeil Armstrong
679e80f906SNeil Armstrong struct sx150x_789_pri {
689e80f906SNeil Armstrong u8 reg_drain;
699e80f906SNeil Armstrong u8 reg_polarity;
709e80f906SNeil Armstrong u8 reg_clock;
719e80f906SNeil Armstrong u8 reg_misc;
729e80f906SNeil Armstrong u8 reg_reset;
739e80f906SNeil Armstrong u8 ngpios;
749e80f906SNeil Armstrong };
759e80f906SNeil Armstrong
769e80f906SNeil Armstrong struct sx150x_device_data {
779e80f906SNeil Armstrong u8 model;
789e80f906SNeil Armstrong u8 reg_pullup;
799e80f906SNeil Armstrong u8 reg_pulldn;
809e80f906SNeil Armstrong u8 reg_dir;
819e80f906SNeil Armstrong u8 reg_data;
829e80f906SNeil Armstrong u8 reg_irq_mask;
839e80f906SNeil Armstrong u8 reg_irq_src;
849e80f906SNeil Armstrong u8 reg_sense;
859e80f906SNeil Armstrong u8 ngpios;
869e80f906SNeil Armstrong union {
879e80f906SNeil Armstrong struct sx150x_123_pri x123;
889e80f906SNeil Armstrong struct sx150x_456_pri x456;
899e80f906SNeil Armstrong struct sx150x_789_pri x789;
909e80f906SNeil Armstrong } pri;
919e80f906SNeil Armstrong const struct pinctrl_pin_desc *pins;
929e80f906SNeil Armstrong unsigned int npins;
939e80f906SNeil Armstrong };
949e80f906SNeil Armstrong
959e80f906SNeil Armstrong struct sx150x_pinctrl {
969e80f906SNeil Armstrong struct device *dev;
979e80f906SNeil Armstrong struct i2c_client *client;
989e80f906SNeil Armstrong struct pinctrl_dev *pctldev;
999e80f906SNeil Armstrong struct pinctrl_desc pinctrl_desc;
1009e80f906SNeil Armstrong struct gpio_chip gpio;
1010db0f26cSAndrey Smirnov struct regmap *regmap;
1029e80f906SNeil Armstrong struct {
1039e80f906SNeil Armstrong u32 sense;
1049e80f906SNeil Armstrong u32 masked;
1059e80f906SNeil Armstrong } irq;
1069e80f906SNeil Armstrong struct mutex lock;
1079e80f906SNeil Armstrong const struct sx150x_device_data *data;
1089e80f906SNeil Armstrong };
1099e80f906SNeil Armstrong
1104f5ac8cfSPeter Rosin static const struct pinctrl_pin_desc sx150x_4_pins[] = {
1114f5ac8cfSPeter Rosin PINCTRL_PIN(0, "gpio0"),
1124f5ac8cfSPeter Rosin PINCTRL_PIN(1, "gpio1"),
1134f5ac8cfSPeter Rosin PINCTRL_PIN(2, "gpio2"),
1144f5ac8cfSPeter Rosin PINCTRL_PIN(3, "gpio3"),
1154f5ac8cfSPeter Rosin PINCTRL_PIN(4, "oscio"),
1164f5ac8cfSPeter Rosin };
1174f5ac8cfSPeter Rosin
1189e80f906SNeil Armstrong static const struct pinctrl_pin_desc sx150x_8_pins[] = {
1199e80f906SNeil Armstrong PINCTRL_PIN(0, "gpio0"),
1209e80f906SNeil Armstrong PINCTRL_PIN(1, "gpio1"),
1219e80f906SNeil Armstrong PINCTRL_PIN(2, "gpio2"),
1229e80f906SNeil Armstrong PINCTRL_PIN(3, "gpio3"),
1239e80f906SNeil Armstrong PINCTRL_PIN(4, "gpio4"),
1249e80f906SNeil Armstrong PINCTRL_PIN(5, "gpio5"),
1259e80f906SNeil Armstrong PINCTRL_PIN(6, "gpio6"),
1269e80f906SNeil Armstrong PINCTRL_PIN(7, "gpio7"),
1279e80f906SNeil Armstrong PINCTRL_PIN(8, "oscio"),
1289e80f906SNeil Armstrong };
1299e80f906SNeil Armstrong
1309e80f906SNeil Armstrong static const struct pinctrl_pin_desc sx150x_16_pins[] = {
1319e80f906SNeil Armstrong PINCTRL_PIN(0, "gpio0"),
1329e80f906SNeil Armstrong PINCTRL_PIN(1, "gpio1"),
1339e80f906SNeil Armstrong PINCTRL_PIN(2, "gpio2"),
1349e80f906SNeil Armstrong PINCTRL_PIN(3, "gpio3"),
1359e80f906SNeil Armstrong PINCTRL_PIN(4, "gpio4"),
1369e80f906SNeil Armstrong PINCTRL_PIN(5, "gpio5"),
1379e80f906SNeil Armstrong PINCTRL_PIN(6, "gpio6"),
1389e80f906SNeil Armstrong PINCTRL_PIN(7, "gpio7"),
1399e80f906SNeil Armstrong PINCTRL_PIN(8, "gpio8"),
1409e80f906SNeil Armstrong PINCTRL_PIN(9, "gpio9"),
1419e80f906SNeil Armstrong PINCTRL_PIN(10, "gpio10"),
1429e80f906SNeil Armstrong PINCTRL_PIN(11, "gpio11"),
1439e80f906SNeil Armstrong PINCTRL_PIN(12, "gpio12"),
1449e80f906SNeil Armstrong PINCTRL_PIN(13, "gpio13"),
1459e80f906SNeil Armstrong PINCTRL_PIN(14, "gpio14"),
1469e80f906SNeil Armstrong PINCTRL_PIN(15, "gpio15"),
1479e80f906SNeil Armstrong PINCTRL_PIN(16, "oscio"),
1489e80f906SNeil Armstrong };
1499e80f906SNeil Armstrong
1504f5ac8cfSPeter Rosin static const struct sx150x_device_data sx1501q_device_data = {
1514f5ac8cfSPeter Rosin .model = SX150X_123,
1524f5ac8cfSPeter Rosin .reg_pullup = 0x02,
1534f5ac8cfSPeter Rosin .reg_pulldn = 0x03,
1544f5ac8cfSPeter Rosin .reg_dir = 0x01,
1554f5ac8cfSPeter Rosin .reg_data = 0x00,
1564f5ac8cfSPeter Rosin .reg_irq_mask = 0x05,
1574f5ac8cfSPeter Rosin .reg_irq_src = 0x08,
1584f5ac8cfSPeter Rosin .reg_sense = 0x07,
1594f5ac8cfSPeter Rosin .pri.x123 = {
1604f5ac8cfSPeter Rosin .reg_pld_mode = 0x10,
1614f5ac8cfSPeter Rosin .reg_pld_table0 = 0x11,
1624f5ac8cfSPeter Rosin .reg_pld_table2 = 0x13,
163c9d26f1aSPeter Rosin .reg_advanced = 0xad,
1644f5ac8cfSPeter Rosin },
1654f5ac8cfSPeter Rosin .ngpios = 4,
1664f5ac8cfSPeter Rosin .pins = sx150x_4_pins,
1674f5ac8cfSPeter Rosin .npins = 4, /* oscio not available */
1684f5ac8cfSPeter Rosin };
1694f5ac8cfSPeter Rosin
1709e80f906SNeil Armstrong static const struct sx150x_device_data sx1502q_device_data = {
1719e80f906SNeil Armstrong .model = SX150X_123,
1729e80f906SNeil Armstrong .reg_pullup = 0x02,
1739e80f906SNeil Armstrong .reg_pulldn = 0x03,
1749e80f906SNeil Armstrong .reg_dir = 0x01,
1759e80f906SNeil Armstrong .reg_data = 0x00,
1769e80f906SNeil Armstrong .reg_irq_mask = 0x05,
1779e80f906SNeil Armstrong .reg_irq_src = 0x08,
1781663682cSPeter Rosin .reg_sense = 0x06,
1799e80f906SNeil Armstrong .pri.x123 = {
1809e80f906SNeil Armstrong .reg_pld_mode = 0x10,
1819e80f906SNeil Armstrong .reg_pld_table0 = 0x11,
1829e80f906SNeil Armstrong .reg_pld_table1 = 0x12,
1839e80f906SNeil Armstrong .reg_pld_table2 = 0x13,
1849e80f906SNeil Armstrong .reg_pld_table3 = 0x14,
1859e80f906SNeil Armstrong .reg_pld_table4 = 0x15,
186c9d26f1aSPeter Rosin .reg_advanced = 0xad,
1879e80f906SNeil Armstrong },
1889e80f906SNeil Armstrong .ngpios = 8,
1899e80f906SNeil Armstrong .pins = sx150x_8_pins,
1909e80f906SNeil Armstrong .npins = 8, /* oscio not available */
1919e80f906SNeil Armstrong };
1929e80f906SNeil Armstrong
1936697546dSAndrey Smirnov static const struct sx150x_device_data sx1503q_device_data = {
1946697546dSAndrey Smirnov .model = SX150X_123,
1956489677fSAndrey Smirnov .reg_pullup = 0x04,
1966489677fSAndrey Smirnov .reg_pulldn = 0x06,
1976489677fSAndrey Smirnov .reg_dir = 0x02,
1986489677fSAndrey Smirnov .reg_data = 0x00,
1996489677fSAndrey Smirnov .reg_irq_mask = 0x08,
2006489677fSAndrey Smirnov .reg_irq_src = 0x0e,
2016489677fSAndrey Smirnov .reg_sense = 0x0a,
2026697546dSAndrey Smirnov .pri.x123 = {
2036489677fSAndrey Smirnov .reg_pld_mode = 0x20,
2046489677fSAndrey Smirnov .reg_pld_table0 = 0x22,
2056489677fSAndrey Smirnov .reg_pld_table1 = 0x24,
2066489677fSAndrey Smirnov .reg_pld_table2 = 0x26,
2076489677fSAndrey Smirnov .reg_pld_table3 = 0x28,
2086489677fSAndrey Smirnov .reg_pld_table4 = 0x2a,
209c9d26f1aSPeter Rosin .reg_advanced = 0xad,
2106697546dSAndrey Smirnov },
2116697546dSAndrey Smirnov .ngpios = 16,
2126697546dSAndrey Smirnov .pins = sx150x_16_pins,
2136697546dSAndrey Smirnov .npins = 16, /* oscio not available */
2146697546dSAndrey Smirnov };
2156697546dSAndrey Smirnov
2164f5ac8cfSPeter Rosin static const struct sx150x_device_data sx1504q_device_data = {
2174f5ac8cfSPeter Rosin .model = SX150X_456,
2184f5ac8cfSPeter Rosin .reg_pullup = 0x02,
2194f5ac8cfSPeter Rosin .reg_pulldn = 0x03,
2204f5ac8cfSPeter Rosin .reg_dir = 0x01,
2214f5ac8cfSPeter Rosin .reg_data = 0x00,
2224f5ac8cfSPeter Rosin .reg_irq_mask = 0x05,
2234f5ac8cfSPeter Rosin .reg_irq_src = 0x08,
2244f5ac8cfSPeter Rosin .reg_sense = 0x07,
2254f5ac8cfSPeter Rosin .pri.x456 = {
2264f5ac8cfSPeter Rosin .reg_pld_mode = 0x10,
2274f5ac8cfSPeter Rosin .reg_pld_table0 = 0x11,
2284f5ac8cfSPeter Rosin .reg_pld_table2 = 0x13,
2294f5ac8cfSPeter Rosin },
2304f5ac8cfSPeter Rosin .ngpios = 4,
2314f5ac8cfSPeter Rosin .pins = sx150x_4_pins,
2324f5ac8cfSPeter Rosin .npins = 4, /* oscio not available */
2334f5ac8cfSPeter Rosin };
2344f5ac8cfSPeter Rosin
2354f5ac8cfSPeter Rosin static const struct sx150x_device_data sx1505q_device_data = {
2364f5ac8cfSPeter Rosin .model = SX150X_456,
2374f5ac8cfSPeter Rosin .reg_pullup = 0x02,
2384f5ac8cfSPeter Rosin .reg_pulldn = 0x03,
2394f5ac8cfSPeter Rosin .reg_dir = 0x01,
2404f5ac8cfSPeter Rosin .reg_data = 0x00,
2414f5ac8cfSPeter Rosin .reg_irq_mask = 0x05,
2424f5ac8cfSPeter Rosin .reg_irq_src = 0x08,
2434f5ac8cfSPeter Rosin .reg_sense = 0x06,
2444f5ac8cfSPeter Rosin .pri.x456 = {
2454f5ac8cfSPeter Rosin .reg_pld_mode = 0x10,
2464f5ac8cfSPeter Rosin .reg_pld_table0 = 0x11,
2474f5ac8cfSPeter Rosin .reg_pld_table1 = 0x12,
2484f5ac8cfSPeter Rosin .reg_pld_table2 = 0x13,
2494f5ac8cfSPeter Rosin .reg_pld_table3 = 0x14,
2504f5ac8cfSPeter Rosin .reg_pld_table4 = 0x15,
2514f5ac8cfSPeter Rosin },
2524f5ac8cfSPeter Rosin .ngpios = 8,
2534f5ac8cfSPeter Rosin .pins = sx150x_8_pins,
2544f5ac8cfSPeter Rosin .npins = 8, /* oscio not available */
2554f5ac8cfSPeter Rosin };
2564f5ac8cfSPeter Rosin
257bba709bdSPeter Rosin static const struct sx150x_device_data sx1506q_device_data = {
258bba709bdSPeter Rosin .model = SX150X_456,
259bba709bdSPeter Rosin .reg_pullup = 0x04,
260bba709bdSPeter Rosin .reg_pulldn = 0x06,
261bba709bdSPeter Rosin .reg_dir = 0x02,
262bba709bdSPeter Rosin .reg_data = 0x00,
263bba709bdSPeter Rosin .reg_irq_mask = 0x08,
264bba709bdSPeter Rosin .reg_irq_src = 0x0e,
265bba709bdSPeter Rosin .reg_sense = 0x0a,
266bba709bdSPeter Rosin .pri.x456 = {
267bba709bdSPeter Rosin .reg_pld_mode = 0x20,
268bba709bdSPeter Rosin .reg_pld_table0 = 0x22,
269bba709bdSPeter Rosin .reg_pld_table1 = 0x24,
270bba709bdSPeter Rosin .reg_pld_table2 = 0x26,
271bba709bdSPeter Rosin .reg_pld_table3 = 0x28,
272bba709bdSPeter Rosin .reg_pld_table4 = 0x2a,
273c9d26f1aSPeter Rosin .reg_advanced = 0xad,
274bba709bdSPeter Rosin },
275bba709bdSPeter Rosin .ngpios = 16,
276bba709bdSPeter Rosin .pins = sx150x_16_pins,
277bba709bdSPeter Rosin .npins = 16, /* oscio not available */
278bba709bdSPeter Rosin };
279bba709bdSPeter Rosin
2804f5ac8cfSPeter Rosin static const struct sx150x_device_data sx1507q_device_data = {
2814f5ac8cfSPeter Rosin .model = SX150X_789,
2824f5ac8cfSPeter Rosin .reg_pullup = 0x03,
2834f5ac8cfSPeter Rosin .reg_pulldn = 0x04,
2844f5ac8cfSPeter Rosin .reg_dir = 0x07,
2854f5ac8cfSPeter Rosin .reg_data = 0x08,
2864f5ac8cfSPeter Rosin .reg_irq_mask = 0x09,
2874f5ac8cfSPeter Rosin .reg_irq_src = 0x0b,
2884f5ac8cfSPeter Rosin .reg_sense = 0x0a,
2894f5ac8cfSPeter Rosin .pri.x789 = {
2904f5ac8cfSPeter Rosin .reg_drain = 0x05,
2914f5ac8cfSPeter Rosin .reg_polarity = 0x06,
2924f5ac8cfSPeter Rosin .reg_clock = 0x0d,
2934f5ac8cfSPeter Rosin .reg_misc = 0x0e,
2944f5ac8cfSPeter Rosin .reg_reset = 0x7d,
2954f5ac8cfSPeter Rosin },
2964f5ac8cfSPeter Rosin .ngpios = 4,
2974f5ac8cfSPeter Rosin .pins = sx150x_4_pins,
2984f5ac8cfSPeter Rosin .npins = ARRAY_SIZE(sx150x_4_pins),
2994f5ac8cfSPeter Rosin };
3004f5ac8cfSPeter Rosin
301bba709bdSPeter Rosin static const struct sx150x_device_data sx1508q_device_data = {
302bba709bdSPeter Rosin .model = SX150X_789,
303bba709bdSPeter Rosin .reg_pullup = 0x03,
304bba709bdSPeter Rosin .reg_pulldn = 0x04,
305bba709bdSPeter Rosin .reg_dir = 0x07,
306bba709bdSPeter Rosin .reg_data = 0x08,
307bba709bdSPeter Rosin .reg_irq_mask = 0x09,
308bba709bdSPeter Rosin .reg_irq_src = 0x0c,
309bba709bdSPeter Rosin .reg_sense = 0x0a,
310bba709bdSPeter Rosin .pri.x789 = {
311bba709bdSPeter Rosin .reg_drain = 0x05,
312bba709bdSPeter Rosin .reg_polarity = 0x06,
313bba709bdSPeter Rosin .reg_clock = 0x0f,
314bba709bdSPeter Rosin .reg_misc = 0x10,
315bba709bdSPeter Rosin .reg_reset = 0x7d,
316bba709bdSPeter Rosin },
317bba709bdSPeter Rosin .ngpios = 8,
318bba709bdSPeter Rosin .pins = sx150x_8_pins,
319bba709bdSPeter Rosin .npins = ARRAY_SIZE(sx150x_8_pins),
320bba709bdSPeter Rosin };
321bba709bdSPeter Rosin
322bba709bdSPeter Rosin static const struct sx150x_device_data sx1509q_device_data = {
323bba709bdSPeter Rosin .model = SX150X_789,
324bba709bdSPeter Rosin .reg_pullup = 0x06,
325bba709bdSPeter Rosin .reg_pulldn = 0x08,
326bba709bdSPeter Rosin .reg_dir = 0x0e,
327bba709bdSPeter Rosin .reg_data = 0x10,
328bba709bdSPeter Rosin .reg_irq_mask = 0x12,
329bba709bdSPeter Rosin .reg_irq_src = 0x18,
330bba709bdSPeter Rosin .reg_sense = 0x14,
331bba709bdSPeter Rosin .pri.x789 = {
332bba709bdSPeter Rosin .reg_drain = 0x0a,
333bba709bdSPeter Rosin .reg_polarity = 0x0c,
334bba709bdSPeter Rosin .reg_clock = 0x1e,
335bba709bdSPeter Rosin .reg_misc = 0x1f,
336bba709bdSPeter Rosin .reg_reset = 0x7d,
337bba709bdSPeter Rosin },
338bba709bdSPeter Rosin .ngpios = 16,
339bba709bdSPeter Rosin .pins = sx150x_16_pins,
340bba709bdSPeter Rosin .npins = ARRAY_SIZE(sx150x_16_pins),
341bba709bdSPeter Rosin };
342bba709bdSPeter Rosin
sx150x_pinctrl_get_groups_count(struct pinctrl_dev * pctldev)3439e80f906SNeil Armstrong static int sx150x_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
3449e80f906SNeil Armstrong {
3459e80f906SNeil Armstrong return 0;
3469e80f906SNeil Armstrong }
3479e80f906SNeil Armstrong
sx150x_pinctrl_get_group_name(struct pinctrl_dev * pctldev,unsigned int group)3489e80f906SNeil Armstrong static const char *sx150x_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
3499e80f906SNeil Armstrong unsigned int group)
3509e80f906SNeil Armstrong {
3519e80f906SNeil Armstrong return NULL;
3529e80f906SNeil Armstrong }
3539e80f906SNeil Armstrong
sx150x_pinctrl_get_group_pins(struct pinctrl_dev * pctldev,unsigned int group,const unsigned int ** pins,unsigned int * num_pins)3549e80f906SNeil Armstrong static int sx150x_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
3559e80f906SNeil Armstrong unsigned int group,
3569e80f906SNeil Armstrong const unsigned int **pins,
3579e80f906SNeil Armstrong unsigned int *num_pins)
3589e80f906SNeil Armstrong {
3599e80f906SNeil Armstrong return -ENOTSUPP;
3609e80f906SNeil Armstrong }
3619e80f906SNeil Armstrong
3629e80f906SNeil Armstrong static const struct pinctrl_ops sx150x_pinctrl_ops = {
3639e80f906SNeil Armstrong .get_groups_count = sx150x_pinctrl_get_groups_count,
3649e80f906SNeil Armstrong .get_group_name = sx150x_pinctrl_get_group_name,
3659e80f906SNeil Armstrong .get_group_pins = sx150x_pinctrl_get_group_pins,
3669e80f906SNeil Armstrong #ifdef CONFIG_OF
3679e80f906SNeil Armstrong .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
3689e80f906SNeil Armstrong .dt_free_map = pinctrl_utils_free_map,
3699e80f906SNeil Armstrong #endif
3709e80f906SNeil Armstrong };
3719e80f906SNeil Armstrong
sx150x_pin_is_oscio(struct sx150x_pinctrl * pctl,unsigned int pin)3729e80f906SNeil Armstrong static bool sx150x_pin_is_oscio(struct sx150x_pinctrl *pctl, unsigned int pin)
3739e80f906SNeil Armstrong {
3749e80f906SNeil Armstrong if (pin >= pctl->data->npins)
3759e80f906SNeil Armstrong return false;
3769e80f906SNeil Armstrong
3779e80f906SNeil Armstrong /* OSCIO pin is only present in 789 devices */
3789e80f906SNeil Armstrong if (pctl->data->model != SX150X_789)
3799e80f906SNeil Armstrong return false;
3809e80f906SNeil Armstrong
3819e80f906SNeil Armstrong return !strcmp(pctl->data->pins[pin].name, "oscio");
3829e80f906SNeil Armstrong }
3839e80f906SNeil Armstrong
sx150x_gpio_get_direction(struct gpio_chip * chip,unsigned int offset)3849e80f906SNeil Armstrong static int sx150x_gpio_get_direction(struct gpio_chip *chip,
3859e80f906SNeil Armstrong unsigned int offset)
3869e80f906SNeil Armstrong {
3879e80f906SNeil Armstrong struct sx150x_pinctrl *pctl = gpiochip_get_data(chip);
3886489677fSAndrey Smirnov unsigned int value;
3896489677fSAndrey Smirnov int ret;
3909e80f906SNeil Armstrong
3919e80f906SNeil Armstrong if (sx150x_pin_is_oscio(pctl, offset))
3923c827873SMatti Vaittinen return GPIO_LINE_DIRECTION_OUT;
3939e80f906SNeil Armstrong
3946489677fSAndrey Smirnov ret = regmap_read(pctl->regmap, pctl->data->reg_dir, &value);
3956489677fSAndrey Smirnov if (ret < 0)
3966489677fSAndrey Smirnov return ret;
3979e80f906SNeil Armstrong
3983c827873SMatti Vaittinen if (value & BIT(offset))
3993c827873SMatti Vaittinen return GPIO_LINE_DIRECTION_IN;
4003c827873SMatti Vaittinen
4013c827873SMatti Vaittinen return GPIO_LINE_DIRECTION_OUT;
4029e80f906SNeil Armstrong }
4039e80f906SNeil Armstrong
sx150x_gpio_get(struct gpio_chip * chip,unsigned int offset)4049e80f906SNeil Armstrong static int sx150x_gpio_get(struct gpio_chip *chip, unsigned int offset)
4059e80f906SNeil Armstrong {
4069e80f906SNeil Armstrong struct sx150x_pinctrl *pctl = gpiochip_get_data(chip);
4076489677fSAndrey Smirnov unsigned int value;
4086489677fSAndrey Smirnov int ret;
4099e80f906SNeil Armstrong
4109e80f906SNeil Armstrong if (sx150x_pin_is_oscio(pctl, offset))
4119e80f906SNeil Armstrong return -EINVAL;
4129e80f906SNeil Armstrong
4136489677fSAndrey Smirnov ret = regmap_read(pctl->regmap, pctl->data->reg_data, &value);
4146489677fSAndrey Smirnov if (ret < 0)
4156489677fSAndrey Smirnov return ret;
4169e80f906SNeil Armstrong
4176489677fSAndrey Smirnov return !!(value & BIT(offset));
4189e80f906SNeil Armstrong }
4199e80f906SNeil Armstrong
__sx150x_gpio_set(struct sx150x_pinctrl * pctl,unsigned int offset,int value)4206489677fSAndrey Smirnov static int __sx150x_gpio_set(struct sx150x_pinctrl *pctl, unsigned int offset,
4216489677fSAndrey Smirnov int value)
4226489677fSAndrey Smirnov {
4236489677fSAndrey Smirnov return regmap_write_bits(pctl->regmap, pctl->data->reg_data,
4246489677fSAndrey Smirnov BIT(offset), value ? BIT(offset) : 0);
4256489677fSAndrey Smirnov }
4266489677fSAndrey Smirnov
sx150x_gpio_oscio_set(struct sx150x_pinctrl * pctl,int value)427ab5bd035SAndrey Smirnov static int sx150x_gpio_oscio_set(struct sx150x_pinctrl *pctl,
428ab5bd035SAndrey Smirnov int value)
429ab5bd035SAndrey Smirnov {
430ab5bd035SAndrey Smirnov return regmap_write(pctl->regmap,
431ab5bd035SAndrey Smirnov pctl->data->pri.x789.reg_clock,
432ab5bd035SAndrey Smirnov (value ? 0x1f : 0x10));
433ab5bd035SAndrey Smirnov }
434ab5bd035SAndrey Smirnov
sx150x_gpio_set(struct gpio_chip * chip,unsigned int offset,int value)4359e80f906SNeil Armstrong static void sx150x_gpio_set(struct gpio_chip *chip, unsigned int offset,
4369e80f906SNeil Armstrong int value)
4379e80f906SNeil Armstrong {
4389e80f906SNeil Armstrong struct sx150x_pinctrl *pctl = gpiochip_get_data(chip);
4399e80f906SNeil Armstrong
440d977a876SAndrey Smirnov if (sx150x_pin_is_oscio(pctl, offset))
441ab5bd035SAndrey Smirnov sx150x_gpio_oscio_set(pctl, value);
442d977a876SAndrey Smirnov else
4436489677fSAndrey Smirnov __sx150x_gpio_set(pctl, offset, value);
4449e80f906SNeil Armstrong }
4459e80f906SNeil Armstrong
sx150x_gpio_set_multiple(struct gpio_chip * chip,unsigned long * mask,unsigned long * bits)446ec61168bSPeter Rosin static void sx150x_gpio_set_multiple(struct gpio_chip *chip,
447ec61168bSPeter Rosin unsigned long *mask,
448ec61168bSPeter Rosin unsigned long *bits)
449ec61168bSPeter Rosin {
450ec61168bSPeter Rosin struct sx150x_pinctrl *pctl = gpiochip_get_data(chip);
451ec61168bSPeter Rosin
452ec61168bSPeter Rosin regmap_write_bits(pctl->regmap, pctl->data->reg_data, *mask, *bits);
453ec61168bSPeter Rosin }
454ec61168bSPeter Rosin
sx150x_gpio_direction_input(struct gpio_chip * chip,unsigned int offset)4559e80f906SNeil Armstrong static int sx150x_gpio_direction_input(struct gpio_chip *chip,
4569e80f906SNeil Armstrong unsigned int offset)
4579e80f906SNeil Armstrong {
4589e80f906SNeil Armstrong struct sx150x_pinctrl *pctl = gpiochip_get_data(chip);
4599e80f906SNeil Armstrong
4609e80f906SNeil Armstrong if (sx150x_pin_is_oscio(pctl, offset))
4619e80f906SNeil Armstrong return -EINVAL;
4629e80f906SNeil Armstrong
463d977a876SAndrey Smirnov return regmap_write_bits(pctl->regmap,
4646489677fSAndrey Smirnov pctl->data->reg_dir,
4656489677fSAndrey Smirnov BIT(offset), BIT(offset));
4669e80f906SNeil Armstrong }
4679e80f906SNeil Armstrong
sx150x_gpio_direction_output(struct gpio_chip * chip,unsigned int offset,int value)4689e80f906SNeil Armstrong static int sx150x_gpio_direction_output(struct gpio_chip *chip,
4699e80f906SNeil Armstrong unsigned int offset, int value)
4709e80f906SNeil Armstrong {
4719e80f906SNeil Armstrong struct sx150x_pinctrl *pctl = gpiochip_get_data(chip);
472d977a876SAndrey Smirnov int ret;
4739e80f906SNeil Armstrong
474ab5bd035SAndrey Smirnov if (sx150x_pin_is_oscio(pctl, offset))
475ab5bd035SAndrey Smirnov return sx150x_gpio_oscio_set(pctl, value);
4769e80f906SNeil Armstrong
477d977a876SAndrey Smirnov ret = __sx150x_gpio_set(pctl, offset, value);
478d977a876SAndrey Smirnov if (ret < 0)
479d977a876SAndrey Smirnov return ret;
480d977a876SAndrey Smirnov
481d977a876SAndrey Smirnov return regmap_write_bits(pctl->regmap,
4826489677fSAndrey Smirnov pctl->data->reg_dir,
4836489677fSAndrey Smirnov BIT(offset), 0);
4849e80f906SNeil Armstrong }
4859e80f906SNeil Armstrong
sx150x_irq_mask(struct irq_data * d)4869e80f906SNeil Armstrong static void sx150x_irq_mask(struct irq_data *d)
4879e80f906SNeil Armstrong {
488df603258SLinus Walleij struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
489df603258SLinus Walleij struct sx150x_pinctrl *pctl = gpiochip_get_data(gc);
490df603258SLinus Walleij unsigned int n = irqd_to_hwirq(d);
4919e80f906SNeil Armstrong
4926489677fSAndrey Smirnov pctl->irq.masked |= BIT(n);
493df603258SLinus Walleij gpiochip_disable_irq(gc, n);
4949e80f906SNeil Armstrong }
4959e80f906SNeil Armstrong
sx150x_irq_unmask(struct irq_data * d)4969e80f906SNeil Armstrong static void sx150x_irq_unmask(struct irq_data *d)
4979e80f906SNeil Armstrong {
498df603258SLinus Walleij struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
499df603258SLinus Walleij struct sx150x_pinctrl *pctl = gpiochip_get_data(gc);
500df603258SLinus Walleij unsigned int n = irqd_to_hwirq(d);
5019e80f906SNeil Armstrong
502df603258SLinus Walleij gpiochip_enable_irq(gc, n);
5036489677fSAndrey Smirnov pctl->irq.masked &= ~BIT(n);
5049e80f906SNeil Armstrong }
5059e80f906SNeil Armstrong
sx150x_irq_set_sense(struct sx150x_pinctrl * pctl,unsigned int line,unsigned int sense)506fd931f23SAndrey Smirnov static void sx150x_irq_set_sense(struct sx150x_pinctrl *pctl,
507fd931f23SAndrey Smirnov unsigned int line, unsigned int sense)
508fd931f23SAndrey Smirnov {
509fd931f23SAndrey Smirnov /*
510fd931f23SAndrey Smirnov * Every interrupt line is represented by two bits shifted
511fd931f23SAndrey Smirnov * proportionally to the line number
512fd931f23SAndrey Smirnov */
513fd931f23SAndrey Smirnov const unsigned int n = line * 2;
514fd931f23SAndrey Smirnov const unsigned int mask = ~((SX150X_IRQ_TYPE_EDGE_RISING |
515fd931f23SAndrey Smirnov SX150X_IRQ_TYPE_EDGE_FALLING) << n);
516fd931f23SAndrey Smirnov
517fd931f23SAndrey Smirnov pctl->irq.sense &= mask;
518fd931f23SAndrey Smirnov pctl->irq.sense |= sense << n;
519fd931f23SAndrey Smirnov }
520fd931f23SAndrey Smirnov
sx150x_irq_set_type(struct irq_data * d,unsigned int flow_type)5219e80f906SNeil Armstrong static int sx150x_irq_set_type(struct irq_data *d, unsigned int flow_type)
5229e80f906SNeil Armstrong {
523df603258SLinus Walleij struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
524df603258SLinus Walleij struct sx150x_pinctrl *pctl = gpiochip_get_data(gc);
5259e80f906SNeil Armstrong unsigned int n, val = 0;
5269e80f906SNeil Armstrong
5279e80f906SNeil Armstrong if (flow_type & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW))
5289e80f906SNeil Armstrong return -EINVAL;
5299e80f906SNeil Armstrong
530df603258SLinus Walleij n = irqd_to_hwirq(d);
5319e80f906SNeil Armstrong
5329e80f906SNeil Armstrong if (flow_type & IRQ_TYPE_EDGE_RISING)
533fd931f23SAndrey Smirnov val |= SX150X_IRQ_TYPE_EDGE_RISING;
5349e80f906SNeil Armstrong if (flow_type & IRQ_TYPE_EDGE_FALLING)
535fd931f23SAndrey Smirnov val |= SX150X_IRQ_TYPE_EDGE_FALLING;
5369e80f906SNeil Armstrong
537fd931f23SAndrey Smirnov sx150x_irq_set_sense(pctl, n, val);
5389e80f906SNeil Armstrong return 0;
5399e80f906SNeil Armstrong }
5409e80f906SNeil Armstrong
sx150x_irq_thread_fn(int irq,void * dev_id)5419e80f906SNeil Armstrong static irqreturn_t sx150x_irq_thread_fn(int irq, void *dev_id)
5429e80f906SNeil Armstrong {
5439e80f906SNeil Armstrong struct sx150x_pinctrl *pctl = (struct sx150x_pinctrl *)dev_id;
54405a90cc7SAndrey Smirnov unsigned long n, status;
5450db0f26cSAndrey Smirnov unsigned int val;
54605a90cc7SAndrey Smirnov int err;
5479e80f906SNeil Armstrong
5486489677fSAndrey Smirnov err = regmap_read(pctl->regmap, pctl->data->reg_irq_src, &val);
5499e80f906SNeil Armstrong if (err < 0)
5506489677fSAndrey Smirnov return IRQ_NONE;
5519e80f906SNeil Armstrong
5526489677fSAndrey Smirnov err = regmap_write(pctl->regmap, pctl->data->reg_irq_src, val);
5539e80f906SNeil Armstrong if (err < 0)
5546489677fSAndrey Smirnov return IRQ_NONE;
5559e80f906SNeil Armstrong
55605a90cc7SAndrey Smirnov status = val;
55705a90cc7SAndrey Smirnov for_each_set_bit(n, &status, pctl->data->ngpios)
558f0fbe7bcSThierry Reding handle_nested_irq(irq_find_mapping(pctl->gpio.irq.domain, n));
5599e80f906SNeil Armstrong
56005a90cc7SAndrey Smirnov return IRQ_HANDLED;
5619e80f906SNeil Armstrong }
5629e80f906SNeil Armstrong
sx150x_irq_bus_lock(struct irq_data * d)5639e80f906SNeil Armstrong static void sx150x_irq_bus_lock(struct irq_data *d)
5649e80f906SNeil Armstrong {
565df603258SLinus Walleij struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
566df603258SLinus Walleij struct sx150x_pinctrl *pctl = gpiochip_get_data(gc);
5679e80f906SNeil Armstrong
5689e80f906SNeil Armstrong mutex_lock(&pctl->lock);
5699e80f906SNeil Armstrong }
5709e80f906SNeil Armstrong
sx150x_irq_bus_sync_unlock(struct irq_data * d)5719e80f906SNeil Armstrong static void sx150x_irq_bus_sync_unlock(struct irq_data *d)
5729e80f906SNeil Armstrong {
573df603258SLinus Walleij struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
574df603258SLinus Walleij struct sx150x_pinctrl *pctl = gpiochip_get_data(gc);
5759e80f906SNeil Armstrong
5766489677fSAndrey Smirnov regmap_write(pctl->regmap, pctl->data->reg_irq_mask, pctl->irq.masked);
5776489677fSAndrey Smirnov regmap_write(pctl->regmap, pctl->data->reg_sense, pctl->irq.sense);
5789e80f906SNeil Armstrong mutex_unlock(&pctl->lock);
5799e80f906SNeil Armstrong }
5809e80f906SNeil Armstrong
581df603258SLinus Walleij
sx150x_irq_print_chip(struct irq_data * d,struct seq_file * p)582df603258SLinus Walleij static void sx150x_irq_print_chip(struct irq_data *d, struct seq_file *p)
583df603258SLinus Walleij {
584df603258SLinus Walleij struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
585df603258SLinus Walleij struct sx150x_pinctrl *pctl = gpiochip_get_data(gc);
586df603258SLinus Walleij
587df603258SLinus Walleij seq_printf(p, pctl->client->name);
588df603258SLinus Walleij }
589df603258SLinus Walleij
590df603258SLinus Walleij static const struct irq_chip sx150x_irq_chip = {
591df603258SLinus Walleij .irq_mask = sx150x_irq_mask,
592df603258SLinus Walleij .irq_unmask = sx150x_irq_unmask,
593df603258SLinus Walleij .irq_set_type = sx150x_irq_set_type,
594df603258SLinus Walleij .irq_bus_lock = sx150x_irq_bus_lock,
595df603258SLinus Walleij .irq_bus_sync_unlock = sx150x_irq_bus_sync_unlock,
596df603258SLinus Walleij .irq_print_chip = sx150x_irq_print_chip,
597df603258SLinus Walleij .flags = IRQCHIP_IMMUTABLE,
598df603258SLinus Walleij GPIOCHIP_IRQ_RESOURCE_HELPERS,
599df603258SLinus Walleij };
600df603258SLinus Walleij
sx150x_pinconf_get(struct pinctrl_dev * pctldev,unsigned int pin,unsigned long * config)6019e80f906SNeil Armstrong static int sx150x_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
6029e80f906SNeil Armstrong unsigned long *config)
6039e80f906SNeil Armstrong {
6049e80f906SNeil Armstrong struct sx150x_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
6059e80f906SNeil Armstrong unsigned int param = pinconf_to_config_param(*config);
6069e80f906SNeil Armstrong int ret;
6079e80f906SNeil Armstrong u32 arg;
6080db0f26cSAndrey Smirnov unsigned int data;
6099e80f906SNeil Armstrong
6106489677fSAndrey Smirnov if (sx150x_pin_is_oscio(pctl, pin)) {
6119e80f906SNeil Armstrong switch (param) {
6129e80f906SNeil Armstrong case PIN_CONFIG_DRIVE_PUSH_PULL:
6139e80f906SNeil Armstrong case PIN_CONFIG_OUTPUT:
6140db0f26cSAndrey Smirnov ret = regmap_read(pctl->regmap,
6159e80f906SNeil Armstrong pctl->data->pri.x789.reg_clock,
6169e80f906SNeil Armstrong &data);
6179e80f906SNeil Armstrong if (ret < 0)
6189e80f906SNeil Armstrong return ret;
6199e80f906SNeil Armstrong
6209e80f906SNeil Armstrong if (param == PIN_CONFIG_DRIVE_PUSH_PULL)
6219e80f906SNeil Armstrong arg = (data & 0x1f) ? 1 : 0;
6229e80f906SNeil Armstrong else {
6239e80f906SNeil Armstrong if ((data & 0x1f) == 0x1f)
6249e80f906SNeil Armstrong arg = 1;
6259e80f906SNeil Armstrong else if ((data & 0x1f) == 0x10)
6269e80f906SNeil Armstrong arg = 0;
6279e80f906SNeil Armstrong else
6289e80f906SNeil Armstrong return -EINVAL;
6299e80f906SNeil Armstrong }
6309e80f906SNeil Armstrong
6319e80f906SNeil Armstrong break;
6329e80f906SNeil Armstrong default:
6339e80f906SNeil Armstrong return -ENOTSUPP;
6349e80f906SNeil Armstrong }
6359e80f906SNeil Armstrong
6369e80f906SNeil Armstrong goto out;
6379e80f906SNeil Armstrong }
6389e80f906SNeil Armstrong
6399e80f906SNeil Armstrong switch (param) {
6409e80f906SNeil Armstrong case PIN_CONFIG_BIAS_PULL_DOWN:
6416489677fSAndrey Smirnov ret = regmap_read(pctl->regmap,
6426489677fSAndrey Smirnov pctl->data->reg_pulldn,
6436489677fSAndrey Smirnov &data);
6446489677fSAndrey Smirnov data &= BIT(pin);
6459e80f906SNeil Armstrong
6469e80f906SNeil Armstrong if (ret < 0)
6479e80f906SNeil Armstrong return ret;
6489e80f906SNeil Armstrong
6499e80f906SNeil Armstrong if (!ret)
6509e80f906SNeil Armstrong return -EINVAL;
6519e80f906SNeil Armstrong
6529e80f906SNeil Armstrong arg = 1;
6539e80f906SNeil Armstrong break;
6549e80f906SNeil Armstrong
6559e80f906SNeil Armstrong case PIN_CONFIG_BIAS_PULL_UP:
6566489677fSAndrey Smirnov ret = regmap_read(pctl->regmap,
6576489677fSAndrey Smirnov pctl->data->reg_pullup,
6586489677fSAndrey Smirnov &data);
6596489677fSAndrey Smirnov data &= BIT(pin);
6609e80f906SNeil Armstrong
6619e80f906SNeil Armstrong if (ret < 0)
6629e80f906SNeil Armstrong return ret;
6639e80f906SNeil Armstrong
6649e80f906SNeil Armstrong if (!ret)
6659e80f906SNeil Armstrong return -EINVAL;
6669e80f906SNeil Armstrong
6679e80f906SNeil Armstrong arg = 1;
6689e80f906SNeil Armstrong break;
6699e80f906SNeil Armstrong
6709e80f906SNeil Armstrong case PIN_CONFIG_DRIVE_OPEN_DRAIN:
6719e80f906SNeil Armstrong if (pctl->data->model != SX150X_789)
6729e80f906SNeil Armstrong return -ENOTSUPP;
6739e80f906SNeil Armstrong
6746489677fSAndrey Smirnov ret = regmap_read(pctl->regmap,
6756489677fSAndrey Smirnov pctl->data->pri.x789.reg_drain,
6766489677fSAndrey Smirnov &data);
6776489677fSAndrey Smirnov data &= BIT(pin);
6789e80f906SNeil Armstrong
6799e80f906SNeil Armstrong if (ret < 0)
6809e80f906SNeil Armstrong return ret;
6819e80f906SNeil Armstrong
6826489677fSAndrey Smirnov if (!data)
6839e80f906SNeil Armstrong return -EINVAL;
6849e80f906SNeil Armstrong
6859e80f906SNeil Armstrong arg = 1;
6869e80f906SNeil Armstrong break;
6879e80f906SNeil Armstrong
6889e80f906SNeil Armstrong case PIN_CONFIG_DRIVE_PUSH_PULL:
6899e80f906SNeil Armstrong if (pctl->data->model != SX150X_789)
6909e80f906SNeil Armstrong arg = true;
6919e80f906SNeil Armstrong else {
6926489677fSAndrey Smirnov ret = regmap_read(pctl->regmap,
6936489677fSAndrey Smirnov pctl->data->pri.x789.reg_drain,
6946489677fSAndrey Smirnov &data);
6956489677fSAndrey Smirnov data &= BIT(pin);
6969e80f906SNeil Armstrong
6979e80f906SNeil Armstrong if (ret < 0)
6989e80f906SNeil Armstrong return ret;
6999e80f906SNeil Armstrong
7006489677fSAndrey Smirnov if (data)
7019e80f906SNeil Armstrong return -EINVAL;
7029e80f906SNeil Armstrong
7039e80f906SNeil Armstrong arg = 1;
7049e80f906SNeil Armstrong }
7059e80f906SNeil Armstrong break;
7069e80f906SNeil Armstrong
7079e80f906SNeil Armstrong case PIN_CONFIG_OUTPUT:
7089e80f906SNeil Armstrong ret = sx150x_gpio_get_direction(&pctl->gpio, pin);
7099e80f906SNeil Armstrong if (ret < 0)
7109e80f906SNeil Armstrong return ret;
7119e80f906SNeil Armstrong
7123c827873SMatti Vaittinen if (ret == GPIO_LINE_DIRECTION_IN)
7139e80f906SNeil Armstrong return -EINVAL;
7149e80f906SNeil Armstrong
7159e80f906SNeil Armstrong ret = sx150x_gpio_get(&pctl->gpio, pin);
7169e80f906SNeil Armstrong if (ret < 0)
7179e80f906SNeil Armstrong return ret;
7189e80f906SNeil Armstrong
7199e80f906SNeil Armstrong arg = ret;
7209e80f906SNeil Armstrong break;
7219e80f906SNeil Armstrong
7229e80f906SNeil Armstrong default:
7239e80f906SNeil Armstrong return -ENOTSUPP;
7249e80f906SNeil Armstrong }
7259e80f906SNeil Armstrong
7269e80f906SNeil Armstrong out:
7279e80f906SNeil Armstrong *config = pinconf_to_config_packed(param, arg);
7289e80f906SNeil Armstrong
7299e80f906SNeil Armstrong return 0;
7309e80f906SNeil Armstrong }
7319e80f906SNeil Armstrong
sx150x_pinconf_set(struct pinctrl_dev * pctldev,unsigned int pin,unsigned long * configs,unsigned int num_configs)7329e80f906SNeil Armstrong static int sx150x_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
7339e80f906SNeil Armstrong unsigned long *configs, unsigned int num_configs)
7349e80f906SNeil Armstrong {
7359e80f906SNeil Armstrong struct sx150x_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
7369e80f906SNeil Armstrong enum pin_config_param param;
7379e80f906SNeil Armstrong u32 arg;
7389e80f906SNeil Armstrong int i;
7399e80f906SNeil Armstrong int ret;
7409e80f906SNeil Armstrong
7419e80f906SNeil Armstrong for (i = 0; i < num_configs; i++) {
7429e80f906SNeil Armstrong param = pinconf_to_config_param(configs[i]);
7439e80f906SNeil Armstrong arg = pinconf_to_config_argument(configs[i]);
7449e80f906SNeil Armstrong
7459e80f906SNeil Armstrong if (sx150x_pin_is_oscio(pctl, pin)) {
7469e80f906SNeil Armstrong if (param == PIN_CONFIG_OUTPUT) {
7479e80f906SNeil Armstrong ret = sx150x_gpio_direction_output(&pctl->gpio,
7489e80f906SNeil Armstrong pin, arg);
7499e80f906SNeil Armstrong if (ret < 0)
7509e80f906SNeil Armstrong return ret;
7519e80f906SNeil Armstrong
7529e80f906SNeil Armstrong continue;
7539e80f906SNeil Armstrong } else
7549e80f906SNeil Armstrong return -ENOTSUPP;
7559e80f906SNeil Armstrong }
7569e80f906SNeil Armstrong
7579e80f906SNeil Armstrong switch (param) {
7589e80f906SNeil Armstrong case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
7599e80f906SNeil Armstrong case PIN_CONFIG_BIAS_DISABLE:
7606489677fSAndrey Smirnov ret = regmap_write_bits(pctl->regmap,
7616489677fSAndrey Smirnov pctl->data->reg_pulldn,
7626489677fSAndrey Smirnov BIT(pin), 0);
7639e80f906SNeil Armstrong if (ret < 0)
7649e80f906SNeil Armstrong return ret;
7659e80f906SNeil Armstrong
7666489677fSAndrey Smirnov ret = regmap_write_bits(pctl->regmap,
7676489677fSAndrey Smirnov pctl->data->reg_pullup,
7686489677fSAndrey Smirnov BIT(pin), 0);
7699e80f906SNeil Armstrong if (ret < 0)
7709e80f906SNeil Armstrong return ret;
7719e80f906SNeil Armstrong
7729e80f906SNeil Armstrong break;
7739e80f906SNeil Armstrong
7749e80f906SNeil Armstrong case PIN_CONFIG_BIAS_PULL_UP:
7756489677fSAndrey Smirnov ret = regmap_write_bits(pctl->regmap,
7769e80f906SNeil Armstrong pctl->data->reg_pullup,
7776489677fSAndrey Smirnov BIT(pin), BIT(pin));
7789e80f906SNeil Armstrong if (ret < 0)
7799e80f906SNeil Armstrong return ret;
7809e80f906SNeil Armstrong
7819e80f906SNeil Armstrong break;
7829e80f906SNeil Armstrong
7839e80f906SNeil Armstrong case PIN_CONFIG_BIAS_PULL_DOWN:
7846489677fSAndrey Smirnov ret = regmap_write_bits(pctl->regmap,
7859e80f906SNeil Armstrong pctl->data->reg_pulldn,
7866489677fSAndrey Smirnov BIT(pin), BIT(pin));
7879e80f906SNeil Armstrong if (ret < 0)
7889e80f906SNeil Armstrong return ret;
7899e80f906SNeil Armstrong
7909e80f906SNeil Armstrong break;
7919e80f906SNeil Armstrong
7929e80f906SNeil Armstrong case PIN_CONFIG_DRIVE_OPEN_DRAIN:
7932956b5d9SMika Westerberg if (pctl->data->model != SX150X_789 ||
7942956b5d9SMika Westerberg sx150x_pin_is_oscio(pctl, pin))
7952956b5d9SMika Westerberg return -ENOTSUPP;
7962956b5d9SMika Westerberg
7972956b5d9SMika Westerberg ret = regmap_write_bits(pctl->regmap,
7982956b5d9SMika Westerberg pctl->data->pri.x789.reg_drain,
7992956b5d9SMika Westerberg BIT(pin), BIT(pin));
8009e80f906SNeil Armstrong if (ret < 0)
8019e80f906SNeil Armstrong return ret;
8029e80f906SNeil Armstrong
8039e80f906SNeil Armstrong break;
8049e80f906SNeil Armstrong
8059e80f906SNeil Armstrong case PIN_CONFIG_DRIVE_PUSH_PULL:
8062956b5d9SMika Westerberg if (pctl->data->model != SX150X_789 ||
8072956b5d9SMika Westerberg sx150x_pin_is_oscio(pctl, pin))
8082956b5d9SMika Westerberg return 0;
8092956b5d9SMika Westerberg
8102956b5d9SMika Westerberg ret = regmap_write_bits(pctl->regmap,
8112956b5d9SMika Westerberg pctl->data->pri.x789.reg_drain,
8122956b5d9SMika Westerberg BIT(pin), 0);
8139e80f906SNeil Armstrong if (ret < 0)
8149e80f906SNeil Armstrong return ret;
8159e80f906SNeil Armstrong
8169e80f906SNeil Armstrong break;
8179e80f906SNeil Armstrong
8189e80f906SNeil Armstrong case PIN_CONFIG_OUTPUT:
8199e80f906SNeil Armstrong ret = sx150x_gpio_direction_output(&pctl->gpio,
8209e80f906SNeil Armstrong pin, arg);
8219e80f906SNeil Armstrong if (ret < 0)
8229e80f906SNeil Armstrong return ret;
8239e80f906SNeil Armstrong
8249e80f906SNeil Armstrong break;
8259e80f906SNeil Armstrong
8269e80f906SNeil Armstrong default:
8279e80f906SNeil Armstrong return -ENOTSUPP;
8289e80f906SNeil Armstrong }
8299e80f906SNeil Armstrong } /* for each config */
8309e80f906SNeil Armstrong
8319e80f906SNeil Armstrong return 0;
8329e80f906SNeil Armstrong }
8339e80f906SNeil Armstrong
8349e80f906SNeil Armstrong static const struct pinconf_ops sx150x_pinconf_ops = {
8359e80f906SNeil Armstrong .pin_config_get = sx150x_pinconf_get,
8369e80f906SNeil Armstrong .pin_config_set = sx150x_pinconf_set,
8379e80f906SNeil Armstrong .is_generic = true,
8389e80f906SNeil Armstrong };
8399e80f906SNeil Armstrong
8409e80f906SNeil Armstrong static const struct i2c_device_id sx150x_id[] = {
8414f5ac8cfSPeter Rosin {"sx1501q", (kernel_ulong_t) &sx1501q_device_data },
8429e80f906SNeil Armstrong {"sx1502q", (kernel_ulong_t) &sx1502q_device_data },
8436697546dSAndrey Smirnov {"sx1503q", (kernel_ulong_t) &sx1503q_device_data },
8444f5ac8cfSPeter Rosin {"sx1504q", (kernel_ulong_t) &sx1504q_device_data },
8454f5ac8cfSPeter Rosin {"sx1505q", (kernel_ulong_t) &sx1505q_device_data },
846bba709bdSPeter Rosin {"sx1506q", (kernel_ulong_t) &sx1506q_device_data },
8474f5ac8cfSPeter Rosin {"sx1507q", (kernel_ulong_t) &sx1507q_device_data },
848bba709bdSPeter Rosin {"sx1508q", (kernel_ulong_t) &sx1508q_device_data },
849bba709bdSPeter Rosin {"sx1509q", (kernel_ulong_t) &sx1509q_device_data },
8509e80f906SNeil Armstrong {}
8519e80f906SNeil Armstrong };
8529e80f906SNeil Armstrong
8539e80f906SNeil Armstrong static const struct of_device_id sx150x_of_match[] = {
8544f5ac8cfSPeter Rosin { .compatible = "semtech,sx1501q", .data = &sx1501q_device_data },
855e3ba8120SAndrey Smirnov { .compatible = "semtech,sx1502q", .data = &sx1502q_device_data },
8566697546dSAndrey Smirnov { .compatible = "semtech,sx1503q", .data = &sx1503q_device_data },
8574f5ac8cfSPeter Rosin { .compatible = "semtech,sx1504q", .data = &sx1504q_device_data },
8584f5ac8cfSPeter Rosin { .compatible = "semtech,sx1505q", .data = &sx1505q_device_data },
859bba709bdSPeter Rosin { .compatible = "semtech,sx1506q", .data = &sx1506q_device_data },
8604f5ac8cfSPeter Rosin { .compatible = "semtech,sx1507q", .data = &sx1507q_device_data },
861bba709bdSPeter Rosin { .compatible = "semtech,sx1508q", .data = &sx1508q_device_data },
862bba709bdSPeter Rosin { .compatible = "semtech,sx1509q", .data = &sx1509q_device_data },
8639e80f906SNeil Armstrong {},
8649e80f906SNeil Armstrong };
8659e80f906SNeil Armstrong
sx150x_reset(struct sx150x_pinctrl * pctl)8669e80f906SNeil Armstrong static int sx150x_reset(struct sx150x_pinctrl *pctl)
8679e80f906SNeil Armstrong {
8689e80f906SNeil Armstrong int err;
8699e80f906SNeil Armstrong
8709e80f906SNeil Armstrong err = i2c_smbus_write_byte_data(pctl->client,
8719e80f906SNeil Armstrong pctl->data->pri.x789.reg_reset,
872f9038d60SAndrey Smirnov SX150X_789_RESET_KEY1);
8739e80f906SNeil Armstrong if (err < 0)
8749e80f906SNeil Armstrong return err;
8759e80f906SNeil Armstrong
8769e80f906SNeil Armstrong err = i2c_smbus_write_byte_data(pctl->client,
8779e80f906SNeil Armstrong pctl->data->pri.x789.reg_reset,
878f9038d60SAndrey Smirnov SX150X_789_RESET_KEY2);
8799e80f906SNeil Armstrong return err;
8809e80f906SNeil Armstrong }
8819e80f906SNeil Armstrong
sx150x_init_misc(struct sx150x_pinctrl * pctl)882310cdfa0SAndrey Smirnov static int sx150x_init_misc(struct sx150x_pinctrl *pctl)
883310cdfa0SAndrey Smirnov {
884310cdfa0SAndrey Smirnov u8 reg, value;
885310cdfa0SAndrey Smirnov
886310cdfa0SAndrey Smirnov switch (pctl->data->model) {
887310cdfa0SAndrey Smirnov case SX150X_789:
888310cdfa0SAndrey Smirnov reg = pctl->data->pri.x789.reg_misc;
889310cdfa0SAndrey Smirnov value = SX150X_789_REG_MISC_AUTOCLEAR_OFF;
890310cdfa0SAndrey Smirnov break;
891310cdfa0SAndrey Smirnov case SX150X_456:
892c9d26f1aSPeter Rosin reg = pctl->data->pri.x456.reg_advanced;
893310cdfa0SAndrey Smirnov value = 0x00;
894b30d31e4SAndrey Smirnov
895b30d31e4SAndrey Smirnov /*
896b30d31e4SAndrey Smirnov * Only SX1506 has RegAdvanced, SX1504/5 are expected
897b30d31e4SAndrey Smirnov * to initialize this offset to zero
898b30d31e4SAndrey Smirnov */
899b30d31e4SAndrey Smirnov if (!reg)
900b30d31e4SAndrey Smirnov return 0;
901310cdfa0SAndrey Smirnov break;
902310cdfa0SAndrey Smirnov case SX150X_123:
903c9d26f1aSPeter Rosin reg = pctl->data->pri.x123.reg_advanced;
904310cdfa0SAndrey Smirnov value = 0x00;
905310cdfa0SAndrey Smirnov break;
906310cdfa0SAndrey Smirnov default:
907310cdfa0SAndrey Smirnov WARN(1, "Unknown chip model %d\n", pctl->data->model);
908310cdfa0SAndrey Smirnov return -EINVAL;
909310cdfa0SAndrey Smirnov }
910310cdfa0SAndrey Smirnov
9116489677fSAndrey Smirnov return regmap_write(pctl->regmap, reg, value);
912310cdfa0SAndrey Smirnov }
913310cdfa0SAndrey Smirnov
sx150x_init_hw(struct sx150x_pinctrl * pctl)9149e80f906SNeil Armstrong static int sx150x_init_hw(struct sx150x_pinctrl *pctl)
9159e80f906SNeil Armstrong {
9166489677fSAndrey Smirnov const u8 reg[] = {
9176489677fSAndrey Smirnov [SX150X_789] = pctl->data->pri.x789.reg_polarity,
9186489677fSAndrey Smirnov [SX150X_456] = pctl->data->pri.x456.reg_pld_mode,
9196489677fSAndrey Smirnov [SX150X_123] = pctl->data->pri.x123.reg_pld_mode,
9206489677fSAndrey Smirnov };
9219e80f906SNeil Armstrong int err;
9229e80f906SNeil Armstrong
9239e80f906SNeil Armstrong if (pctl->data->model == SX150X_789 &&
9249e80f906SNeil Armstrong of_property_read_bool(pctl->dev->of_node, "semtech,probe-reset")) {
9259e80f906SNeil Armstrong err = sx150x_reset(pctl);
9269e80f906SNeil Armstrong if (err < 0)
9279e80f906SNeil Armstrong return err;
9289e80f906SNeil Armstrong }
9299e80f906SNeil Armstrong
930310cdfa0SAndrey Smirnov err = sx150x_init_misc(pctl);
9319e80f906SNeil Armstrong if (err < 0)
9329e80f906SNeil Armstrong return err;
9339e80f906SNeil Armstrong
9349e80f906SNeil Armstrong /* Set all pins to work in normal mode */
9356489677fSAndrey Smirnov return regmap_write(pctl->regmap, reg[pctl->data->model], 0);
9369e80f906SNeil Armstrong }
9379e80f906SNeil Armstrong
sx150x_regmap_reg_width(struct sx150x_pinctrl * pctl,unsigned int reg)9386489677fSAndrey Smirnov static int sx150x_regmap_reg_width(struct sx150x_pinctrl *pctl,
9396489677fSAndrey Smirnov unsigned int reg)
9406489677fSAndrey Smirnov {
9416489677fSAndrey Smirnov const struct sx150x_device_data *data = pctl->data;
9426489677fSAndrey Smirnov
9436489677fSAndrey Smirnov if (reg == data->reg_sense) {
9446489677fSAndrey Smirnov /*
9456489677fSAndrey Smirnov * RegSense packs two bits of configuration per GPIO,
9466489677fSAndrey Smirnov * so we'd need to read twice as many bits as there
9476489677fSAndrey Smirnov * are GPIO in our chip
9486489677fSAndrey Smirnov */
9496489677fSAndrey Smirnov return 2 * data->ngpios;
9506489677fSAndrey Smirnov } else if ((data->model == SX150X_789 &&
9516489677fSAndrey Smirnov (reg == data->pri.x789.reg_misc ||
9526489677fSAndrey Smirnov reg == data->pri.x789.reg_clock ||
9536489677fSAndrey Smirnov reg == data->pri.x789.reg_reset))
9546489677fSAndrey Smirnov ||
9556489677fSAndrey Smirnov (data->model == SX150X_123 &&
956c9d26f1aSPeter Rosin reg == data->pri.x123.reg_advanced)
9576489677fSAndrey Smirnov ||
9586489677fSAndrey Smirnov (data->model == SX150X_456 &&
959283dc0beSPeter Rosin data->pri.x456.reg_advanced &&
960c9d26f1aSPeter Rosin reg == data->pri.x456.reg_advanced)) {
9616489677fSAndrey Smirnov return 8;
9626489677fSAndrey Smirnov } else {
9636489677fSAndrey Smirnov return data->ngpios;
9646489677fSAndrey Smirnov }
9656489677fSAndrey Smirnov }
9666489677fSAndrey Smirnov
sx150x_maybe_swizzle(struct sx150x_pinctrl * pctl,unsigned int reg,unsigned int val)9676489677fSAndrey Smirnov static unsigned int sx150x_maybe_swizzle(struct sx150x_pinctrl *pctl,
9686489677fSAndrey Smirnov unsigned int reg, unsigned int val)
9696489677fSAndrey Smirnov {
9706489677fSAndrey Smirnov unsigned int a, b;
9716489677fSAndrey Smirnov const struct sx150x_device_data *data = pctl->data;
9726489677fSAndrey Smirnov
9736489677fSAndrey Smirnov /*
9746489677fSAndrey Smirnov * Whereas SX1509 presents RegSense in a simple layout as such:
9756489677fSAndrey Smirnov * reg [ f f e e d d c c ]
9766489677fSAndrey Smirnov * reg + 1 [ b b a a 9 9 8 8 ]
9776489677fSAndrey Smirnov * reg + 2 [ 7 7 6 6 5 5 4 4 ]
9786489677fSAndrey Smirnov * reg + 3 [ 3 3 2 2 1 1 0 0 ]
9796489677fSAndrey Smirnov *
9806489677fSAndrey Smirnov * SX1503 and SX1506 deviate from that data layout, instead storing
9817bd47496SPeter Rosin * their contents as follows:
9826489677fSAndrey Smirnov *
9836489677fSAndrey Smirnov * reg [ f f e e d d c c ]
9846489677fSAndrey Smirnov * reg + 1 [ 7 7 6 6 5 5 4 4 ]
9856489677fSAndrey Smirnov * reg + 2 [ b b a a 9 9 8 8 ]
9866489677fSAndrey Smirnov * reg + 3 [ 3 3 2 2 1 1 0 0 ]
9876489677fSAndrey Smirnov *
9886489677fSAndrey Smirnov * so, taking that into account, we swap two
9896489677fSAndrey Smirnov * inner bytes of a 4-byte result
9906489677fSAndrey Smirnov */
9916489677fSAndrey Smirnov
9926489677fSAndrey Smirnov if (reg == data->reg_sense &&
9936489677fSAndrey Smirnov data->ngpios == 16 &&
9946489677fSAndrey Smirnov (data->model == SX150X_123 ||
9956489677fSAndrey Smirnov data->model == SX150X_456)) {
9966489677fSAndrey Smirnov a = val & 0x00ff0000;
9976489677fSAndrey Smirnov b = val & 0x0000ff00;
9986489677fSAndrey Smirnov
9996489677fSAndrey Smirnov val &= 0xff0000ff;
10006489677fSAndrey Smirnov val |= b << 8;
10016489677fSAndrey Smirnov val |= a >> 8;
10026489677fSAndrey Smirnov }
10036489677fSAndrey Smirnov
10046489677fSAndrey Smirnov return val;
10056489677fSAndrey Smirnov }
10066489677fSAndrey Smirnov
10076489677fSAndrey Smirnov /*
10086489677fSAndrey Smirnov * In order to mask the differences between 16 and 8 bit expander
10096489677fSAndrey Smirnov * devices we set up a sligthly ficticious regmap that pretends to be
1010d71ffeb9SDejin Zheng * a set of 32-bit (to accommodate RegSenseLow/RegSenseHigh
10116489677fSAndrey Smirnov * pair/quartet) registers and transparently reconstructs those
10126489677fSAndrey Smirnov * registers via multiple I2C/SMBus reads
10136489677fSAndrey Smirnov *
10146489677fSAndrey Smirnov * This way the rest of the driver code, interfacing with the chip via
10156489677fSAndrey Smirnov * regmap API, can work assuming that each GPIO pin is represented by
10167bd47496SPeter Rosin * a group of bits at an offset proportional to GPIO number within a
10176489677fSAndrey Smirnov * given register.
10186489677fSAndrey Smirnov */
sx150x_regmap_reg_read(void * context,unsigned int reg,unsigned int * result)10196489677fSAndrey Smirnov static int sx150x_regmap_reg_read(void *context, unsigned int reg,
10206489677fSAndrey Smirnov unsigned int *result)
10216489677fSAndrey Smirnov {
10226489677fSAndrey Smirnov int ret, n;
10236489677fSAndrey Smirnov struct sx150x_pinctrl *pctl = context;
10246489677fSAndrey Smirnov struct i2c_client *i2c = pctl->client;
10256489677fSAndrey Smirnov const int width = sx150x_regmap_reg_width(pctl, reg);
10266489677fSAndrey Smirnov unsigned int idx, val;
10276489677fSAndrey Smirnov
10286489677fSAndrey Smirnov /*
10297bd47496SPeter Rosin * There are four potential cases covered by this function:
10306489677fSAndrey Smirnov *
10316489677fSAndrey Smirnov * 1) 8-pin chip, single configuration bit register
10326489677fSAndrey Smirnov *
10336489677fSAndrey Smirnov * This is trivial the code below just needs to read:
10346489677fSAndrey Smirnov * reg [ 7 6 5 4 3 2 1 0 ]
10356489677fSAndrey Smirnov *
10366489677fSAndrey Smirnov * 2) 8-pin chip, double configuration bit register (RegSense)
10376489677fSAndrey Smirnov *
10386489677fSAndrey Smirnov * The read will be done as follows:
10396489677fSAndrey Smirnov * reg [ 7 7 6 6 5 5 4 4 ]
10406489677fSAndrey Smirnov * reg + 1 [ 3 3 2 2 1 1 0 0 ]
10416489677fSAndrey Smirnov *
10426489677fSAndrey Smirnov * 3) 16-pin chip, single configuration bit register
10436489677fSAndrey Smirnov *
10446489677fSAndrey Smirnov * The read will be done as follows:
10456489677fSAndrey Smirnov * reg [ f e d c b a 9 8 ]
10466489677fSAndrey Smirnov * reg + 1 [ 7 6 5 4 3 2 1 0 ]
10476489677fSAndrey Smirnov *
10486489677fSAndrey Smirnov * 4) 16-pin chip, double configuration bit register (RegSense)
10496489677fSAndrey Smirnov *
10506489677fSAndrey Smirnov * The read will be done as follows:
10516489677fSAndrey Smirnov * reg [ f f e e d d c c ]
10526489677fSAndrey Smirnov * reg + 1 [ b b a a 9 9 8 8 ]
10536489677fSAndrey Smirnov * reg + 2 [ 7 7 6 6 5 5 4 4 ]
10546489677fSAndrey Smirnov * reg + 3 [ 3 3 2 2 1 1 0 0 ]
10556489677fSAndrey Smirnov */
10566489677fSAndrey Smirnov
10576489677fSAndrey Smirnov for (n = width, val = 0, idx = reg; n > 0; n -= 8, idx++) {
10586489677fSAndrey Smirnov val <<= 8;
10596489677fSAndrey Smirnov
10606489677fSAndrey Smirnov ret = i2c_smbus_read_byte_data(i2c, idx);
10616489677fSAndrey Smirnov if (ret < 0)
10626489677fSAndrey Smirnov return ret;
10636489677fSAndrey Smirnov
10646489677fSAndrey Smirnov val |= ret;
10656489677fSAndrey Smirnov }
10666489677fSAndrey Smirnov
10676489677fSAndrey Smirnov *result = sx150x_maybe_swizzle(pctl, reg, val);
10686489677fSAndrey Smirnov
10696489677fSAndrey Smirnov return 0;
10706489677fSAndrey Smirnov }
10716489677fSAndrey Smirnov
sx150x_regmap_reg_write(void * context,unsigned int reg,unsigned int val)10726489677fSAndrey Smirnov static int sx150x_regmap_reg_write(void *context, unsigned int reg,
10736489677fSAndrey Smirnov unsigned int val)
10746489677fSAndrey Smirnov {
10756489677fSAndrey Smirnov int ret, n;
10766489677fSAndrey Smirnov struct sx150x_pinctrl *pctl = context;
10776489677fSAndrey Smirnov struct i2c_client *i2c = pctl->client;
10786489677fSAndrey Smirnov const int width = sx150x_regmap_reg_width(pctl, reg);
10796489677fSAndrey Smirnov
10806489677fSAndrey Smirnov val = sx150x_maybe_swizzle(pctl, reg, val);
10816489677fSAndrey Smirnov
10826c4ef627SPeter Rosin n = (width - 1) & ~7;
10836489677fSAndrey Smirnov do {
10846489677fSAndrey Smirnov const u8 byte = (val >> n) & 0xff;
10856489677fSAndrey Smirnov
10866489677fSAndrey Smirnov ret = i2c_smbus_write_byte_data(i2c, reg, byte);
10876489677fSAndrey Smirnov if (ret < 0)
10886489677fSAndrey Smirnov return ret;
10896489677fSAndrey Smirnov
10906489677fSAndrey Smirnov reg++;
10916489677fSAndrey Smirnov n -= 8;
10926489677fSAndrey Smirnov } while (n >= 0);
10936489677fSAndrey Smirnov
10949e80f906SNeil Armstrong return 0;
10959e80f906SNeil Armstrong }
10969e80f906SNeil Armstrong
sx150x_reg_volatile(struct device * dev,unsigned int reg)10970db0f26cSAndrey Smirnov static bool sx150x_reg_volatile(struct device *dev, unsigned int reg)
10980db0f26cSAndrey Smirnov {
10990db0f26cSAndrey Smirnov struct sx150x_pinctrl *pctl = i2c_get_clientdata(to_i2c_client(dev));
11000db0f26cSAndrey Smirnov
11016489677fSAndrey Smirnov return reg == pctl->data->reg_irq_src || reg == pctl->data->reg_data;
11020db0f26cSAndrey Smirnov }
11030db0f26cSAndrey Smirnov
11041356d86fSColin Ian King static const struct regmap_config sx150x_regmap_config = {
11050db0f26cSAndrey Smirnov .reg_bits = 8,
11066489677fSAndrey Smirnov .val_bits = 32,
11070db0f26cSAndrey Smirnov
11080db0f26cSAndrey Smirnov .cache_type = REGCACHE_RBTREE,
11090db0f26cSAndrey Smirnov
11106489677fSAndrey Smirnov .reg_read = sx150x_regmap_reg_read,
11116489677fSAndrey Smirnov .reg_write = sx150x_regmap_reg_write,
11126489677fSAndrey Smirnov
11130db0f26cSAndrey Smirnov .max_register = SX150X_MAX_REGISTER,
11140db0f26cSAndrey Smirnov .volatile_reg = sx150x_reg_volatile,
11150db0f26cSAndrey Smirnov };
11160db0f26cSAndrey Smirnov
sx150x_probe(struct i2c_client * client)1117542c893cSUwe Kleine-König static int sx150x_probe(struct i2c_client *client)
11189e80f906SNeil Armstrong {
1119542c893cSUwe Kleine-König const struct i2c_device_id *id = i2c_client_get_device_id(client);
11209e80f906SNeil Armstrong static const u32 i2c_funcs = I2C_FUNC_SMBUS_BYTE_DATA |
11219e80f906SNeil Armstrong I2C_FUNC_SMBUS_WRITE_WORD_DATA;
11229e80f906SNeil Armstrong struct device *dev = &client->dev;
11239e80f906SNeil Armstrong struct sx150x_pinctrl *pctl;
11249e80f906SNeil Armstrong int ret;
11259e80f906SNeil Armstrong
11269e80f906SNeil Armstrong if (!i2c_check_functionality(client->adapter, i2c_funcs))
11279e80f906SNeil Armstrong return -ENOSYS;
11289e80f906SNeil Armstrong
11299e80f906SNeil Armstrong pctl = devm_kzalloc(dev, sizeof(*pctl), GFP_KERNEL);
11309e80f906SNeil Armstrong if (!pctl)
11319e80f906SNeil Armstrong return -ENOMEM;
11329e80f906SNeil Armstrong
11330db0f26cSAndrey Smirnov i2c_set_clientdata(client, pctl);
11340db0f26cSAndrey Smirnov
11359e80f906SNeil Armstrong pctl->dev = dev;
11369e80f906SNeil Armstrong pctl->client = client;
1137e3ba8120SAndrey Smirnov
1138e3ba8120SAndrey Smirnov if (dev->of_node)
1139e3ba8120SAndrey Smirnov pctl->data = of_device_get_match_data(dev);
1140e3ba8120SAndrey Smirnov else
1141e3ba8120SAndrey Smirnov pctl->data = (struct sx150x_device_data *)id->driver_data;
1142e3ba8120SAndrey Smirnov
1143e3ba8120SAndrey Smirnov if (!pctl->data)
1144e3ba8120SAndrey Smirnov return -EINVAL;
11459e80f906SNeil Armstrong
11466489677fSAndrey Smirnov pctl->regmap = devm_regmap_init(dev, NULL, pctl,
11476489677fSAndrey Smirnov &sx150x_regmap_config);
11480db0f26cSAndrey Smirnov if (IS_ERR(pctl->regmap)) {
11490db0f26cSAndrey Smirnov ret = PTR_ERR(pctl->regmap);
11500db0f26cSAndrey Smirnov dev_err(dev, "Failed to allocate register map: %d\n",
11510db0f26cSAndrey Smirnov ret);
11520db0f26cSAndrey Smirnov return ret;
11530db0f26cSAndrey Smirnov }
11540db0f26cSAndrey Smirnov
11559e80f906SNeil Armstrong mutex_init(&pctl->lock);
11569e80f906SNeil Armstrong
11579e80f906SNeil Armstrong ret = sx150x_init_hw(pctl);
11589e80f906SNeil Armstrong if (ret)
11599e80f906SNeil Armstrong return ret;
11609e80f906SNeil Armstrong
11611a1d39e1SPeter Rosin /* Pinctrl_desc */
11621a1d39e1SPeter Rosin pctl->pinctrl_desc.name = "sx150x-pinctrl";
11631a1d39e1SPeter Rosin pctl->pinctrl_desc.pctlops = &sx150x_pinctrl_ops;
11641a1d39e1SPeter Rosin pctl->pinctrl_desc.confops = &sx150x_pinconf_ops;
11651a1d39e1SPeter Rosin pctl->pinctrl_desc.pins = pctl->data->pins;
11661a1d39e1SPeter Rosin pctl->pinctrl_desc.npins = pctl->data->npins;
11671a1d39e1SPeter Rosin pctl->pinctrl_desc.owner = THIS_MODULE;
11681a1d39e1SPeter Rosin
11691a1d39e1SPeter Rosin ret = devm_pinctrl_register_and_init(dev, &pctl->pinctrl_desc,
11701a1d39e1SPeter Rosin pctl, &pctl->pctldev);
11711a1d39e1SPeter Rosin if (ret) {
11721a1d39e1SPeter Rosin dev_err(dev, "Failed to register pinctrl device\n");
11731a1d39e1SPeter Rosin return ret;
11741a1d39e1SPeter Rosin }
11751a1d39e1SPeter Rosin
11769e80f906SNeil Armstrong /* Register GPIO controller */
11779e80f906SNeil Armstrong pctl->gpio.base = -1;
11789e80f906SNeil Armstrong pctl->gpio.ngpio = pctl->data->npins;
11799e80f906SNeil Armstrong pctl->gpio.get_direction = sx150x_gpio_get_direction;
11809e80f906SNeil Armstrong pctl->gpio.direction_input = sx150x_gpio_direction_input;
11819e80f906SNeil Armstrong pctl->gpio.direction_output = sx150x_gpio_direction_output;
11829e80f906SNeil Armstrong pctl->gpio.get = sx150x_gpio_get;
11839e80f906SNeil Armstrong pctl->gpio.set = sx150x_gpio_set;
11842956b5d9SMika Westerberg pctl->gpio.set_config = gpiochip_generic_config;
11859e80f906SNeil Armstrong pctl->gpio.parent = dev;
11869e80f906SNeil Armstrong pctl->gpio.can_sleep = true;
1187a9d9f6b8SNicholas Mc Guire pctl->gpio.label = devm_kstrdup(dev, client->name, GFP_KERNEL);
1188a9d9f6b8SNicholas Mc Guire if (!pctl->gpio.label)
1189a9d9f6b8SNicholas Mc Guire return -ENOMEM;
1190a9d9f6b8SNicholas Mc Guire
1191ec61168bSPeter Rosin /*
1192ec61168bSPeter Rosin * Setting multiple pins is not safe when all pins are not
1193ec61168bSPeter Rosin * handled by the same regmap register. The oscio pin (present
1194ec61168bSPeter Rosin * on the SX150X_789 chips) lives in its own register, so
1195ec61168bSPeter Rosin * would require locking that is not in place at this time.
1196ec61168bSPeter Rosin */
1197ec61168bSPeter Rosin if (pctl->data->model != SX150X_789)
1198ec61168bSPeter Rosin pctl->gpio.set_multiple = sx150x_gpio_set_multiple;
11999e80f906SNeil Armstrong
12009e80f906SNeil Armstrong /* Add Interrupt support if an irq is specified */
12019e80f906SNeil Armstrong if (client->irq > 0) {
12020a04d767SLinus Walleij struct gpio_irq_chip *girq;
12030a04d767SLinus Walleij
12049e80f906SNeil Armstrong pctl->irq.masked = ~0;
12059e80f906SNeil Armstrong pctl->irq.sense = 0;
1206080c489dSAndrey Smirnov /*
1207080c489dSAndrey Smirnov * Because sx150x_irq_threaded_fn invokes all of the
12080a04d767SLinus Walleij * nested interrupt handlers via handle_nested_irq,
12090a04d767SLinus Walleij * any "handler" assigned to struct gpio_irq_chip
1210080c489dSAndrey Smirnov * below is going to be ignored, so the choice of the
1211080c489dSAndrey Smirnov * function does not matter that much.
1212080c489dSAndrey Smirnov *
1213080c489dSAndrey Smirnov * We set it to handle_bad_irq to avoid confusion,
1214080c489dSAndrey Smirnov * plus it will be instantly noticeable if it is ever
1215080c489dSAndrey Smirnov * called (should not happen)
1216080c489dSAndrey Smirnov */
12170a04d767SLinus Walleij girq = &pctl->gpio.irq;
1218df603258SLinus Walleij gpio_irq_chip_set_chip(girq, &sx150x_irq_chip);
12190a04d767SLinus Walleij /* This will let us handle the parent IRQ in the driver */
12200a04d767SLinus Walleij girq->parent_handler = NULL;
12210a04d767SLinus Walleij girq->num_parents = 0;
12220a04d767SLinus Walleij girq->parents = NULL;
12230a04d767SLinus Walleij girq->default_type = IRQ_TYPE_NONE;
12240a04d767SLinus Walleij girq->handler = handle_bad_irq;
12250a04d767SLinus Walleij girq->threaded = true;
12269e80f906SNeil Armstrong
12279e80f906SNeil Armstrong ret = devm_request_threaded_irq(dev, client->irq, NULL,
12289e80f906SNeil Armstrong sx150x_irq_thread_fn,
12299e80f906SNeil Armstrong IRQF_ONESHOT | IRQF_SHARED |
12309e80f906SNeil Armstrong IRQF_TRIGGER_FALLING,
1231df603258SLinus Walleij client->name, pctl);
12329e80f906SNeil Armstrong if (ret < 0)
12339e80f906SNeil Armstrong return ret;
12349e80f906SNeil Armstrong }
12359e80f906SNeil Armstrong
12360a04d767SLinus Walleij ret = devm_gpiochip_add_data(dev, &pctl->gpio, pctl);
12370a04d767SLinus Walleij if (ret)
12380a04d767SLinus Walleij return ret;
12390a04d767SLinus Walleij
12406d8e04f9SMartin DEVERA /*
12416d8e04f9SMartin DEVERA * Pin control functions need to be enabled AFTER registering the
12426d8e04f9SMartin DEVERA * GPIO chip because sx150x_pinconf_set() calls
12436d8e04f9SMartin DEVERA * sx150x_gpio_direction_output().
12446d8e04f9SMartin DEVERA */
12456d8e04f9SMartin DEVERA ret = pinctrl_enable(pctl->pctldev);
12466d8e04f9SMartin DEVERA if (ret) {
12476d8e04f9SMartin DEVERA dev_err(dev, "Failed to enable pinctrl device\n");
12486d8e04f9SMartin DEVERA return ret;
12496d8e04f9SMartin DEVERA }
12506d8e04f9SMartin DEVERA
12510a04d767SLinus Walleij ret = gpiochip_add_pin_range(&pctl->gpio, dev_name(dev),
12520a04d767SLinus Walleij 0, 0, pctl->data->npins);
12530a04d767SLinus Walleij if (ret)
12540a04d767SLinus Walleij return ret;
12550a04d767SLinus Walleij
12569e80f906SNeil Armstrong return 0;
12579e80f906SNeil Armstrong }
12589e80f906SNeil Armstrong
12599e80f906SNeil Armstrong static struct i2c_driver sx150x_driver = {
12609e80f906SNeil Armstrong .driver = {
12619e80f906SNeil Armstrong .name = "sx150x-pinctrl",
12623f2d4560SKrzysztof Kozlowski .of_match_table = sx150x_of_match,
12639e80f906SNeil Armstrong },
1264*d8572531SUwe Kleine-König .probe = sx150x_probe,
12659e80f906SNeil Armstrong .id_table = sx150x_id,
12669e80f906SNeil Armstrong };
12679e80f906SNeil Armstrong
sx150x_init(void)12689e80f906SNeil Armstrong static int __init sx150x_init(void)
12699e80f906SNeil Armstrong {
12709e80f906SNeil Armstrong return i2c_add_driver(&sx150x_driver);
12719e80f906SNeil Armstrong }
12729e80f906SNeil Armstrong subsys_initcall(sx150x_init);
1273