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 1199e80f906SNeil Armstrong static const struct pinctrl_pin_desc sx150x_8_pins[] = { 1209e80f906SNeil Armstrong PINCTRL_PIN(0, "gpio0"), 1219e80f906SNeil Armstrong PINCTRL_PIN(1, "gpio1"), 1229e80f906SNeil Armstrong PINCTRL_PIN(2, "gpio2"), 1239e80f906SNeil Armstrong PINCTRL_PIN(3, "gpio3"), 1249e80f906SNeil Armstrong PINCTRL_PIN(4, "gpio4"), 1259e80f906SNeil Armstrong PINCTRL_PIN(5, "gpio5"), 1269e80f906SNeil Armstrong PINCTRL_PIN(6, "gpio6"), 1279e80f906SNeil Armstrong PINCTRL_PIN(7, "gpio7"), 1289e80f906SNeil Armstrong PINCTRL_PIN(8, "oscio"), 1299e80f906SNeil Armstrong }; 1309e80f906SNeil Armstrong 1319e80f906SNeil Armstrong static const struct pinctrl_pin_desc sx150x_16_pins[] = { 1329e80f906SNeil Armstrong PINCTRL_PIN(0, "gpio0"), 1339e80f906SNeil Armstrong PINCTRL_PIN(1, "gpio1"), 1349e80f906SNeil Armstrong PINCTRL_PIN(2, "gpio2"), 1359e80f906SNeil Armstrong PINCTRL_PIN(3, "gpio3"), 1369e80f906SNeil Armstrong PINCTRL_PIN(4, "gpio4"), 1379e80f906SNeil Armstrong PINCTRL_PIN(5, "gpio5"), 1389e80f906SNeil Armstrong PINCTRL_PIN(6, "gpio6"), 1399e80f906SNeil Armstrong PINCTRL_PIN(7, "gpio7"), 1409e80f906SNeil Armstrong PINCTRL_PIN(8, "gpio8"), 1419e80f906SNeil Armstrong PINCTRL_PIN(9, "gpio9"), 1429e80f906SNeil Armstrong PINCTRL_PIN(10, "gpio10"), 1439e80f906SNeil Armstrong PINCTRL_PIN(11, "gpio11"), 1449e80f906SNeil Armstrong PINCTRL_PIN(12, "gpio12"), 1459e80f906SNeil Armstrong PINCTRL_PIN(13, "gpio13"), 1469e80f906SNeil Armstrong PINCTRL_PIN(14, "gpio14"), 1479e80f906SNeil Armstrong PINCTRL_PIN(15, "gpio15"), 1489e80f906SNeil Armstrong PINCTRL_PIN(16, "oscio"), 1499e80f906SNeil Armstrong }; 1509e80f906SNeil Armstrong 1519e80f906SNeil Armstrong static const struct sx150x_device_data sx1508q_device_data = { 1529e80f906SNeil Armstrong .model = SX150X_789, 1539e80f906SNeil Armstrong .reg_pullup = 0x03, 1549e80f906SNeil Armstrong .reg_pulldn = 0x04, 1559e80f906SNeil Armstrong .reg_dir = 0x07, 1569e80f906SNeil Armstrong .reg_data = 0x08, 1579e80f906SNeil Armstrong .reg_irq_mask = 0x09, 1589e80f906SNeil Armstrong .reg_irq_src = 0x0c, 1599e80f906SNeil Armstrong .reg_sense = 0x0b, 1609e80f906SNeil Armstrong .pri.x789 = { 1619e80f906SNeil Armstrong .reg_drain = 0x05, 1629e80f906SNeil Armstrong .reg_polarity = 0x06, 1639e80f906SNeil Armstrong .reg_clock = 0x0f, 1649e80f906SNeil Armstrong .reg_misc = 0x10, 1659e80f906SNeil Armstrong .reg_reset = 0x7d, 1669e80f906SNeil Armstrong }, 1679e80f906SNeil Armstrong .ngpios = 8, 1689e80f906SNeil Armstrong .pins = sx150x_8_pins, 1699e80f906SNeil Armstrong .npins = ARRAY_SIZE(sx150x_8_pins), 1709e80f906SNeil Armstrong }; 1719e80f906SNeil Armstrong 1729e80f906SNeil Armstrong static const struct sx150x_device_data sx1509q_device_data = { 1739e80f906SNeil Armstrong .model = SX150X_789, 1746489677fSAndrey Smirnov .reg_pullup = 0x06, 1756489677fSAndrey Smirnov .reg_pulldn = 0x08, 1766489677fSAndrey Smirnov .reg_dir = 0x0e, 1776489677fSAndrey Smirnov .reg_data = 0x10, 1786489677fSAndrey Smirnov .reg_irq_mask = 0x12, 1796489677fSAndrey Smirnov .reg_irq_src = 0x18, 1806489677fSAndrey Smirnov .reg_sense = 0x14, 1819e80f906SNeil Armstrong .pri.x789 = { 1826489677fSAndrey Smirnov .reg_drain = 0x0a, 1836489677fSAndrey Smirnov .reg_polarity = 0x0c, 1849e80f906SNeil Armstrong .reg_clock = 0x1e, 1859e80f906SNeil Armstrong .reg_misc = 0x1f, 1869e80f906SNeil Armstrong .reg_reset = 0x7d, 1879e80f906SNeil Armstrong }, 1889e80f906SNeil Armstrong .ngpios = 16, 1899e80f906SNeil Armstrong .pins = sx150x_16_pins, 1909e80f906SNeil Armstrong .npins = ARRAY_SIZE(sx150x_16_pins), 1919e80f906SNeil Armstrong }; 1929e80f906SNeil Armstrong 1939e80f906SNeil Armstrong static const struct sx150x_device_data sx1506q_device_data = { 1949e80f906SNeil Armstrong .model = SX150X_456, 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, 2029e80f906SNeil Armstrong .pri.x456 = { 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, 2099e80f906SNeil Armstrong .reg_advance = 0xad, 2109e80f906SNeil Armstrong }, 2119e80f906SNeil Armstrong .ngpios = 16, 2129e80f906SNeil Armstrong .pins = sx150x_16_pins, 2139e80f906SNeil Armstrong .npins = 16, /* oscio not available */ 2149e80f906SNeil Armstrong }; 2159e80f906SNeil Armstrong 2169e80f906SNeil Armstrong static const struct sx150x_device_data sx1502q_device_data = { 2179e80f906SNeil Armstrong .model = SX150X_123, 2189e80f906SNeil Armstrong .reg_pullup = 0x02, 2199e80f906SNeil Armstrong .reg_pulldn = 0x03, 2209e80f906SNeil Armstrong .reg_dir = 0x01, 2219e80f906SNeil Armstrong .reg_data = 0x00, 2229e80f906SNeil Armstrong .reg_irq_mask = 0x05, 2239e80f906SNeil Armstrong .reg_irq_src = 0x08, 2249e80f906SNeil Armstrong .reg_sense = 0x07, 2259e80f906SNeil Armstrong .pri.x123 = { 2269e80f906SNeil Armstrong .reg_pld_mode = 0x10, 2279e80f906SNeil Armstrong .reg_pld_table0 = 0x11, 2289e80f906SNeil Armstrong .reg_pld_table1 = 0x12, 2299e80f906SNeil Armstrong .reg_pld_table2 = 0x13, 2309e80f906SNeil Armstrong .reg_pld_table3 = 0x14, 2319e80f906SNeil Armstrong .reg_pld_table4 = 0x15, 2329e80f906SNeil Armstrong .reg_advance = 0xad, 2339e80f906SNeil Armstrong }, 2349e80f906SNeil Armstrong .ngpios = 8, 2359e80f906SNeil Armstrong .pins = sx150x_8_pins, 2369e80f906SNeil Armstrong .npins = 8, /* oscio not available */ 2379e80f906SNeil Armstrong }; 2389e80f906SNeil Armstrong 2396697546dSAndrey Smirnov static const struct sx150x_device_data sx1503q_device_data = { 2406697546dSAndrey Smirnov .model = SX150X_123, 2416489677fSAndrey Smirnov .reg_pullup = 0x04, 2426489677fSAndrey Smirnov .reg_pulldn = 0x06, 2436489677fSAndrey Smirnov .reg_dir = 0x02, 2446489677fSAndrey Smirnov .reg_data = 0x00, 2456489677fSAndrey Smirnov .reg_irq_mask = 0x08, 2466489677fSAndrey Smirnov .reg_irq_src = 0x0e, 2476489677fSAndrey Smirnov .reg_sense = 0x0a, 2486697546dSAndrey Smirnov .pri.x123 = { 2496489677fSAndrey Smirnov .reg_pld_mode = 0x20, 2506489677fSAndrey Smirnov .reg_pld_table0 = 0x22, 2516489677fSAndrey Smirnov .reg_pld_table1 = 0x24, 2526489677fSAndrey Smirnov .reg_pld_table2 = 0x26, 2536489677fSAndrey Smirnov .reg_pld_table3 = 0x28, 2546489677fSAndrey Smirnov .reg_pld_table4 = 0x2a, 2556697546dSAndrey Smirnov .reg_advance = 0xad, 2566697546dSAndrey Smirnov }, 2576697546dSAndrey Smirnov .ngpios = 16, 2586697546dSAndrey Smirnov .pins = sx150x_16_pins, 2596697546dSAndrey Smirnov .npins = 16, /* oscio not available */ 2606697546dSAndrey Smirnov }; 2616697546dSAndrey Smirnov 2629e80f906SNeil Armstrong static int sx150x_pinctrl_get_groups_count(struct pinctrl_dev *pctldev) 2639e80f906SNeil Armstrong { 2649e80f906SNeil Armstrong return 0; 2659e80f906SNeil Armstrong } 2669e80f906SNeil Armstrong 2679e80f906SNeil Armstrong static const char *sx150x_pinctrl_get_group_name(struct pinctrl_dev *pctldev, 2689e80f906SNeil Armstrong unsigned int group) 2699e80f906SNeil Armstrong { 2709e80f906SNeil Armstrong return NULL; 2719e80f906SNeil Armstrong } 2729e80f906SNeil Armstrong 2739e80f906SNeil Armstrong static int sx150x_pinctrl_get_group_pins(struct pinctrl_dev *pctldev, 2749e80f906SNeil Armstrong unsigned int group, 2759e80f906SNeil Armstrong const unsigned int **pins, 2769e80f906SNeil Armstrong unsigned int *num_pins) 2779e80f906SNeil Armstrong { 2789e80f906SNeil Armstrong return -ENOTSUPP; 2799e80f906SNeil Armstrong } 2809e80f906SNeil Armstrong 2819e80f906SNeil Armstrong static const struct pinctrl_ops sx150x_pinctrl_ops = { 2829e80f906SNeil Armstrong .get_groups_count = sx150x_pinctrl_get_groups_count, 2839e80f906SNeil Armstrong .get_group_name = sx150x_pinctrl_get_group_name, 2849e80f906SNeil Armstrong .get_group_pins = sx150x_pinctrl_get_group_pins, 2859e80f906SNeil Armstrong #ifdef CONFIG_OF 2869e80f906SNeil Armstrong .dt_node_to_map = pinconf_generic_dt_node_to_map_pin, 2879e80f906SNeil Armstrong .dt_free_map = pinctrl_utils_free_map, 2889e80f906SNeil Armstrong #endif 2899e80f906SNeil Armstrong }; 2909e80f906SNeil Armstrong 2919e80f906SNeil Armstrong static bool sx150x_pin_is_oscio(struct sx150x_pinctrl *pctl, unsigned int pin) 2929e80f906SNeil Armstrong { 2939e80f906SNeil Armstrong if (pin >= pctl->data->npins) 2949e80f906SNeil Armstrong return false; 2959e80f906SNeil Armstrong 2969e80f906SNeil Armstrong /* OSCIO pin is only present in 789 devices */ 2979e80f906SNeil Armstrong if (pctl->data->model != SX150X_789) 2989e80f906SNeil Armstrong return false; 2999e80f906SNeil Armstrong 3009e80f906SNeil Armstrong return !strcmp(pctl->data->pins[pin].name, "oscio"); 3019e80f906SNeil Armstrong } 3029e80f906SNeil Armstrong 3039e80f906SNeil Armstrong static int sx150x_gpio_get_direction(struct gpio_chip *chip, 3049e80f906SNeil Armstrong unsigned int offset) 3059e80f906SNeil Armstrong { 3069e80f906SNeil Armstrong struct sx150x_pinctrl *pctl = gpiochip_get_data(chip); 3076489677fSAndrey Smirnov unsigned int value; 3086489677fSAndrey Smirnov int ret; 3099e80f906SNeil Armstrong 3109e80f906SNeil Armstrong if (sx150x_pin_is_oscio(pctl, offset)) 3119e80f906SNeil Armstrong return false; 3129e80f906SNeil Armstrong 3136489677fSAndrey Smirnov ret = regmap_read(pctl->regmap, pctl->data->reg_dir, &value); 3146489677fSAndrey Smirnov if (ret < 0) 3156489677fSAndrey Smirnov return ret; 3169e80f906SNeil Armstrong 3176489677fSAndrey Smirnov return !!(value & BIT(offset)); 3189e80f906SNeil Armstrong } 3199e80f906SNeil Armstrong 3209e80f906SNeil Armstrong static int sx150x_gpio_get(struct gpio_chip *chip, unsigned int offset) 3219e80f906SNeil Armstrong { 3229e80f906SNeil Armstrong struct sx150x_pinctrl *pctl = gpiochip_get_data(chip); 3236489677fSAndrey Smirnov unsigned int value; 3246489677fSAndrey Smirnov int ret; 3259e80f906SNeil Armstrong 3269e80f906SNeil Armstrong if (sx150x_pin_is_oscio(pctl, offset)) 3279e80f906SNeil Armstrong return -EINVAL; 3289e80f906SNeil Armstrong 3296489677fSAndrey Smirnov ret = regmap_read(pctl->regmap, pctl->data->reg_data, &value); 3306489677fSAndrey Smirnov if (ret < 0) 3316489677fSAndrey Smirnov return ret; 3329e80f906SNeil Armstrong 3336489677fSAndrey Smirnov return !!(value & BIT(offset)); 3349e80f906SNeil Armstrong } 3359e80f906SNeil Armstrong 3369e80f906SNeil Armstrong static int sx150x_gpio_set_single_ended(struct gpio_chip *chip, 3379e80f906SNeil Armstrong unsigned int offset, 3389e80f906SNeil Armstrong enum single_ended_mode mode) 3399e80f906SNeil Armstrong { 3409e80f906SNeil Armstrong struct sx150x_pinctrl *pctl = gpiochip_get_data(chip); 3419e80f906SNeil Armstrong int ret; 3429e80f906SNeil Armstrong 3439e80f906SNeil Armstrong switch (mode) { 3449e80f906SNeil Armstrong case LINE_MODE_PUSH_PULL: 3459e80f906SNeil Armstrong if (pctl->data->model != SX150X_789 || 3469e80f906SNeil Armstrong sx150x_pin_is_oscio(pctl, offset)) 3479e80f906SNeil Armstrong return 0; 3489e80f906SNeil Armstrong 3496489677fSAndrey Smirnov ret = regmap_write_bits(pctl->regmap, 3509e80f906SNeil Armstrong pctl->data->pri.x789.reg_drain, 3516489677fSAndrey Smirnov BIT(offset), 0); 3529e80f906SNeil Armstrong break; 3539e80f906SNeil Armstrong 3549e80f906SNeil Armstrong case LINE_MODE_OPEN_DRAIN: 3559e80f906SNeil Armstrong if (pctl->data->model != SX150X_789 || 3569e80f906SNeil Armstrong sx150x_pin_is_oscio(pctl, offset)) 3579e80f906SNeil Armstrong return -ENOTSUPP; 3589e80f906SNeil Armstrong 3596489677fSAndrey Smirnov ret = regmap_write_bits(pctl->regmap, 3609e80f906SNeil Armstrong pctl->data->pri.x789.reg_drain, 3616489677fSAndrey Smirnov BIT(offset), BIT(offset)); 3629e80f906SNeil Armstrong break; 3639e80f906SNeil Armstrong default: 364d977a876SAndrey Smirnov ret = -ENOTSUPP; 365d977a876SAndrey Smirnov break; 3669e80f906SNeil Armstrong } 3679e80f906SNeil Armstrong 368d977a876SAndrey Smirnov return ret; 3699e80f906SNeil Armstrong } 3709e80f906SNeil Armstrong 3716489677fSAndrey Smirnov static int __sx150x_gpio_set(struct sx150x_pinctrl *pctl, unsigned int offset, 3726489677fSAndrey Smirnov int value) 3736489677fSAndrey Smirnov { 3746489677fSAndrey Smirnov return regmap_write_bits(pctl->regmap, pctl->data->reg_data, 3756489677fSAndrey Smirnov BIT(offset), value ? BIT(offset) : 0); 3766489677fSAndrey Smirnov } 3776489677fSAndrey Smirnov 378ab5bd035SAndrey Smirnov static int sx150x_gpio_oscio_set(struct sx150x_pinctrl *pctl, 379ab5bd035SAndrey Smirnov int value) 380ab5bd035SAndrey Smirnov { 381ab5bd035SAndrey Smirnov return regmap_write(pctl->regmap, 382ab5bd035SAndrey Smirnov pctl->data->pri.x789.reg_clock, 383ab5bd035SAndrey Smirnov (value ? 0x1f : 0x10)); 384ab5bd035SAndrey Smirnov } 385ab5bd035SAndrey Smirnov 3869e80f906SNeil Armstrong static void sx150x_gpio_set(struct gpio_chip *chip, unsigned int offset, 3879e80f906SNeil Armstrong int value) 3889e80f906SNeil Armstrong { 3899e80f906SNeil Armstrong struct sx150x_pinctrl *pctl = gpiochip_get_data(chip); 3909e80f906SNeil Armstrong 391d977a876SAndrey Smirnov if (sx150x_pin_is_oscio(pctl, offset)) 392ab5bd035SAndrey Smirnov sx150x_gpio_oscio_set(pctl, value); 393d977a876SAndrey Smirnov else 3946489677fSAndrey Smirnov __sx150x_gpio_set(pctl, offset, value); 395d977a876SAndrey Smirnov 3969e80f906SNeil Armstrong } 3979e80f906SNeil Armstrong 398*ec61168bSPeter Rosin static void sx150x_gpio_set_multiple(struct gpio_chip *chip, 399*ec61168bSPeter Rosin unsigned long *mask, 400*ec61168bSPeter Rosin unsigned long *bits) 401*ec61168bSPeter Rosin { 402*ec61168bSPeter Rosin struct sx150x_pinctrl *pctl = gpiochip_get_data(chip); 403*ec61168bSPeter Rosin 404*ec61168bSPeter Rosin regmap_write_bits(pctl->regmap, pctl->data->reg_data, *mask, *bits); 405*ec61168bSPeter Rosin } 406*ec61168bSPeter Rosin 4079e80f906SNeil Armstrong static int sx150x_gpio_direction_input(struct gpio_chip *chip, 4089e80f906SNeil Armstrong unsigned int offset) 4099e80f906SNeil Armstrong { 4109e80f906SNeil Armstrong struct sx150x_pinctrl *pctl = gpiochip_get_data(chip); 4119e80f906SNeil Armstrong 4129e80f906SNeil Armstrong if (sx150x_pin_is_oscio(pctl, offset)) 4139e80f906SNeil Armstrong return -EINVAL; 4149e80f906SNeil Armstrong 415d977a876SAndrey Smirnov return regmap_write_bits(pctl->regmap, 4166489677fSAndrey Smirnov pctl->data->reg_dir, 4176489677fSAndrey Smirnov BIT(offset), BIT(offset)); 4189e80f906SNeil Armstrong } 4199e80f906SNeil Armstrong 4209e80f906SNeil Armstrong static int sx150x_gpio_direction_output(struct gpio_chip *chip, 4219e80f906SNeil Armstrong unsigned int offset, int value) 4229e80f906SNeil Armstrong { 4239e80f906SNeil Armstrong struct sx150x_pinctrl *pctl = gpiochip_get_data(chip); 424d977a876SAndrey Smirnov int ret; 4259e80f906SNeil Armstrong 426ab5bd035SAndrey Smirnov if (sx150x_pin_is_oscio(pctl, offset)) 427ab5bd035SAndrey Smirnov return sx150x_gpio_oscio_set(pctl, value); 4289e80f906SNeil Armstrong 429d977a876SAndrey Smirnov ret = __sx150x_gpio_set(pctl, offset, value); 430d977a876SAndrey Smirnov if (ret < 0) 431d977a876SAndrey Smirnov return ret; 432d977a876SAndrey Smirnov 433d977a876SAndrey Smirnov return regmap_write_bits(pctl->regmap, 4346489677fSAndrey Smirnov pctl->data->reg_dir, 4356489677fSAndrey Smirnov BIT(offset), 0); 4369e80f906SNeil Armstrong } 4379e80f906SNeil Armstrong 4389e80f906SNeil Armstrong static void sx150x_irq_mask(struct irq_data *d) 4399e80f906SNeil Armstrong { 4409e80f906SNeil Armstrong struct sx150x_pinctrl *pctl = 4419e80f906SNeil Armstrong gpiochip_get_data(irq_data_get_irq_chip_data(d)); 4429e80f906SNeil Armstrong unsigned int n = d->hwirq; 4439e80f906SNeil Armstrong 4446489677fSAndrey Smirnov pctl->irq.masked |= BIT(n); 4459e80f906SNeil Armstrong } 4469e80f906SNeil Armstrong 4479e80f906SNeil Armstrong static void sx150x_irq_unmask(struct irq_data *d) 4489e80f906SNeil Armstrong { 4499e80f906SNeil Armstrong struct sx150x_pinctrl *pctl = 4509e80f906SNeil Armstrong gpiochip_get_data(irq_data_get_irq_chip_data(d)); 4519e80f906SNeil Armstrong unsigned int n = d->hwirq; 4529e80f906SNeil Armstrong 4536489677fSAndrey Smirnov pctl->irq.masked &= ~BIT(n); 4549e80f906SNeil Armstrong } 4559e80f906SNeil Armstrong 456fd931f23SAndrey Smirnov static void sx150x_irq_set_sense(struct sx150x_pinctrl *pctl, 457fd931f23SAndrey Smirnov unsigned int line, unsigned int sense) 458fd931f23SAndrey Smirnov { 459fd931f23SAndrey Smirnov /* 460fd931f23SAndrey Smirnov * Every interrupt line is represented by two bits shifted 461fd931f23SAndrey Smirnov * proportionally to the line number 462fd931f23SAndrey Smirnov */ 463fd931f23SAndrey Smirnov const unsigned int n = line * 2; 464fd931f23SAndrey Smirnov const unsigned int mask = ~((SX150X_IRQ_TYPE_EDGE_RISING | 465fd931f23SAndrey Smirnov SX150X_IRQ_TYPE_EDGE_FALLING) << n); 466fd931f23SAndrey Smirnov 467fd931f23SAndrey Smirnov pctl->irq.sense &= mask; 468fd931f23SAndrey Smirnov pctl->irq.sense |= sense << n; 469fd931f23SAndrey Smirnov } 470fd931f23SAndrey Smirnov 4719e80f906SNeil Armstrong static int sx150x_irq_set_type(struct irq_data *d, unsigned int flow_type) 4729e80f906SNeil Armstrong { 4739e80f906SNeil Armstrong struct sx150x_pinctrl *pctl = 4749e80f906SNeil Armstrong gpiochip_get_data(irq_data_get_irq_chip_data(d)); 4759e80f906SNeil Armstrong unsigned int n, val = 0; 4769e80f906SNeil Armstrong 4779e80f906SNeil Armstrong if (flow_type & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) 4789e80f906SNeil Armstrong return -EINVAL; 4799e80f906SNeil Armstrong 4809e80f906SNeil Armstrong n = d->hwirq; 4819e80f906SNeil Armstrong 4829e80f906SNeil Armstrong if (flow_type & IRQ_TYPE_EDGE_RISING) 483fd931f23SAndrey Smirnov val |= SX150X_IRQ_TYPE_EDGE_RISING; 4849e80f906SNeil Armstrong if (flow_type & IRQ_TYPE_EDGE_FALLING) 485fd931f23SAndrey Smirnov val |= SX150X_IRQ_TYPE_EDGE_FALLING; 4869e80f906SNeil Armstrong 487fd931f23SAndrey Smirnov sx150x_irq_set_sense(pctl, n, val); 4889e80f906SNeil Armstrong return 0; 4899e80f906SNeil Armstrong } 4909e80f906SNeil Armstrong 4919e80f906SNeil Armstrong static irqreturn_t sx150x_irq_thread_fn(int irq, void *dev_id) 4929e80f906SNeil Armstrong { 4939e80f906SNeil Armstrong struct sx150x_pinctrl *pctl = (struct sx150x_pinctrl *)dev_id; 49405a90cc7SAndrey Smirnov unsigned long n, status; 4950db0f26cSAndrey Smirnov unsigned int val; 49605a90cc7SAndrey Smirnov int err; 4979e80f906SNeil Armstrong 4986489677fSAndrey Smirnov err = regmap_read(pctl->regmap, pctl->data->reg_irq_src, &val); 4999e80f906SNeil Armstrong if (err < 0) 5006489677fSAndrey Smirnov return IRQ_NONE; 5019e80f906SNeil Armstrong 5026489677fSAndrey Smirnov err = regmap_write(pctl->regmap, pctl->data->reg_irq_src, val); 5039e80f906SNeil Armstrong if (err < 0) 5046489677fSAndrey Smirnov return IRQ_NONE; 5059e80f906SNeil Armstrong 50605a90cc7SAndrey Smirnov status = val; 50705a90cc7SAndrey Smirnov for_each_set_bit(n, &status, pctl->data->ngpios) 50805a90cc7SAndrey Smirnov handle_nested_irq(irq_find_mapping(pctl->gpio.irqdomain, n)); 5099e80f906SNeil Armstrong 51005a90cc7SAndrey Smirnov return IRQ_HANDLED; 5119e80f906SNeil Armstrong } 5129e80f906SNeil Armstrong 5139e80f906SNeil Armstrong static void sx150x_irq_bus_lock(struct irq_data *d) 5149e80f906SNeil Armstrong { 5159e80f906SNeil Armstrong struct sx150x_pinctrl *pctl = 5169e80f906SNeil Armstrong gpiochip_get_data(irq_data_get_irq_chip_data(d)); 5179e80f906SNeil Armstrong 5189e80f906SNeil Armstrong mutex_lock(&pctl->lock); 5199e80f906SNeil Armstrong } 5209e80f906SNeil Armstrong 5219e80f906SNeil Armstrong static void sx150x_irq_bus_sync_unlock(struct irq_data *d) 5229e80f906SNeil Armstrong { 5239e80f906SNeil Armstrong struct sx150x_pinctrl *pctl = 5249e80f906SNeil Armstrong gpiochip_get_data(irq_data_get_irq_chip_data(d)); 5259e80f906SNeil Armstrong 5266489677fSAndrey Smirnov regmap_write(pctl->regmap, pctl->data->reg_irq_mask, pctl->irq.masked); 5276489677fSAndrey Smirnov regmap_write(pctl->regmap, pctl->data->reg_sense, pctl->irq.sense); 5289e80f906SNeil Armstrong mutex_unlock(&pctl->lock); 5299e80f906SNeil Armstrong } 5309e80f906SNeil Armstrong 5319e80f906SNeil Armstrong static int sx150x_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin, 5329e80f906SNeil Armstrong unsigned long *config) 5339e80f906SNeil Armstrong { 5349e80f906SNeil Armstrong struct sx150x_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev); 5359e80f906SNeil Armstrong unsigned int param = pinconf_to_config_param(*config); 5369e80f906SNeil Armstrong int ret; 5379e80f906SNeil Armstrong u32 arg; 5380db0f26cSAndrey Smirnov unsigned int data; 5399e80f906SNeil Armstrong 5406489677fSAndrey Smirnov if (sx150x_pin_is_oscio(pctl, pin)) { 5419e80f906SNeil Armstrong switch (param) { 5429e80f906SNeil Armstrong case PIN_CONFIG_DRIVE_PUSH_PULL: 5439e80f906SNeil Armstrong case PIN_CONFIG_OUTPUT: 5440db0f26cSAndrey Smirnov ret = regmap_read(pctl->regmap, 5459e80f906SNeil Armstrong pctl->data->pri.x789.reg_clock, 5469e80f906SNeil Armstrong &data); 5479e80f906SNeil Armstrong if (ret < 0) 5489e80f906SNeil Armstrong return ret; 5499e80f906SNeil Armstrong 5509e80f906SNeil Armstrong if (param == PIN_CONFIG_DRIVE_PUSH_PULL) 5519e80f906SNeil Armstrong arg = (data & 0x1f) ? 1 : 0; 5529e80f906SNeil Armstrong else { 5539e80f906SNeil Armstrong if ((data & 0x1f) == 0x1f) 5549e80f906SNeil Armstrong arg = 1; 5559e80f906SNeil Armstrong else if ((data & 0x1f) == 0x10) 5569e80f906SNeil Armstrong arg = 0; 5579e80f906SNeil Armstrong else 5589e80f906SNeil Armstrong return -EINVAL; 5599e80f906SNeil Armstrong } 5609e80f906SNeil Armstrong 5619e80f906SNeil Armstrong break; 5629e80f906SNeil Armstrong default: 5639e80f906SNeil Armstrong return -ENOTSUPP; 5649e80f906SNeil Armstrong } 5659e80f906SNeil Armstrong 5669e80f906SNeil Armstrong goto out; 5679e80f906SNeil Armstrong } 5689e80f906SNeil Armstrong 5699e80f906SNeil Armstrong switch (param) { 5709e80f906SNeil Armstrong case PIN_CONFIG_BIAS_PULL_DOWN: 5716489677fSAndrey Smirnov ret = regmap_read(pctl->regmap, 5726489677fSAndrey Smirnov pctl->data->reg_pulldn, 5736489677fSAndrey Smirnov &data); 5746489677fSAndrey Smirnov data &= BIT(pin); 5759e80f906SNeil Armstrong 5769e80f906SNeil Armstrong if (ret < 0) 5779e80f906SNeil Armstrong return ret; 5789e80f906SNeil Armstrong 5799e80f906SNeil Armstrong if (!ret) 5809e80f906SNeil Armstrong return -EINVAL; 5819e80f906SNeil Armstrong 5829e80f906SNeil Armstrong arg = 1; 5839e80f906SNeil Armstrong break; 5849e80f906SNeil Armstrong 5859e80f906SNeil Armstrong case PIN_CONFIG_BIAS_PULL_UP: 5866489677fSAndrey Smirnov ret = regmap_read(pctl->regmap, 5876489677fSAndrey Smirnov pctl->data->reg_pullup, 5886489677fSAndrey Smirnov &data); 5896489677fSAndrey Smirnov data &= BIT(pin); 5909e80f906SNeil Armstrong 5919e80f906SNeil Armstrong if (ret < 0) 5929e80f906SNeil Armstrong return ret; 5939e80f906SNeil Armstrong 5949e80f906SNeil Armstrong if (!ret) 5959e80f906SNeil Armstrong return -EINVAL; 5969e80f906SNeil Armstrong 5979e80f906SNeil Armstrong arg = 1; 5989e80f906SNeil Armstrong break; 5999e80f906SNeil Armstrong 6009e80f906SNeil Armstrong case PIN_CONFIG_DRIVE_OPEN_DRAIN: 6019e80f906SNeil Armstrong if (pctl->data->model != SX150X_789) 6029e80f906SNeil Armstrong return -ENOTSUPP; 6039e80f906SNeil Armstrong 6046489677fSAndrey Smirnov ret = regmap_read(pctl->regmap, 6056489677fSAndrey Smirnov pctl->data->pri.x789.reg_drain, 6066489677fSAndrey Smirnov &data); 6076489677fSAndrey Smirnov data &= BIT(pin); 6089e80f906SNeil Armstrong 6099e80f906SNeil Armstrong if (ret < 0) 6109e80f906SNeil Armstrong return ret; 6119e80f906SNeil Armstrong 6126489677fSAndrey Smirnov if (!data) 6139e80f906SNeil Armstrong return -EINVAL; 6149e80f906SNeil Armstrong 6159e80f906SNeil Armstrong arg = 1; 6169e80f906SNeil Armstrong break; 6179e80f906SNeil Armstrong 6189e80f906SNeil Armstrong case PIN_CONFIG_DRIVE_PUSH_PULL: 6199e80f906SNeil Armstrong if (pctl->data->model != SX150X_789) 6209e80f906SNeil Armstrong arg = true; 6219e80f906SNeil Armstrong else { 6226489677fSAndrey Smirnov ret = regmap_read(pctl->regmap, 6236489677fSAndrey Smirnov pctl->data->pri.x789.reg_drain, 6246489677fSAndrey Smirnov &data); 6256489677fSAndrey Smirnov data &= BIT(pin); 6269e80f906SNeil Armstrong 6279e80f906SNeil Armstrong if (ret < 0) 6289e80f906SNeil Armstrong return ret; 6299e80f906SNeil Armstrong 6306489677fSAndrey Smirnov if (data) 6319e80f906SNeil Armstrong return -EINVAL; 6329e80f906SNeil Armstrong 6339e80f906SNeil Armstrong arg = 1; 6349e80f906SNeil Armstrong } 6359e80f906SNeil Armstrong break; 6369e80f906SNeil Armstrong 6379e80f906SNeil Armstrong case PIN_CONFIG_OUTPUT: 6389e80f906SNeil Armstrong ret = sx150x_gpio_get_direction(&pctl->gpio, pin); 6399e80f906SNeil Armstrong if (ret < 0) 6409e80f906SNeil Armstrong return ret; 6419e80f906SNeil Armstrong 6429e80f906SNeil Armstrong if (ret) 6439e80f906SNeil Armstrong return -EINVAL; 6449e80f906SNeil Armstrong 6459e80f906SNeil Armstrong ret = sx150x_gpio_get(&pctl->gpio, pin); 6469e80f906SNeil Armstrong if (ret < 0) 6479e80f906SNeil Armstrong return ret; 6489e80f906SNeil Armstrong 6499e80f906SNeil Armstrong arg = ret; 6509e80f906SNeil Armstrong break; 6519e80f906SNeil Armstrong 6529e80f906SNeil Armstrong default: 6539e80f906SNeil Armstrong return -ENOTSUPP; 6549e80f906SNeil Armstrong } 6559e80f906SNeil Armstrong 6569e80f906SNeil Armstrong out: 6579e80f906SNeil Armstrong *config = pinconf_to_config_packed(param, arg); 6589e80f906SNeil Armstrong 6599e80f906SNeil Armstrong return 0; 6609e80f906SNeil Armstrong } 6619e80f906SNeil Armstrong 6629e80f906SNeil Armstrong static int sx150x_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin, 6639e80f906SNeil Armstrong unsigned long *configs, unsigned int num_configs) 6649e80f906SNeil Armstrong { 6659e80f906SNeil Armstrong struct sx150x_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev); 6669e80f906SNeil Armstrong enum pin_config_param param; 6679e80f906SNeil Armstrong u32 arg; 6689e80f906SNeil Armstrong int i; 6699e80f906SNeil Armstrong int ret; 6709e80f906SNeil Armstrong 6719e80f906SNeil Armstrong for (i = 0; i < num_configs; i++) { 6729e80f906SNeil Armstrong param = pinconf_to_config_param(configs[i]); 6739e80f906SNeil Armstrong arg = pinconf_to_config_argument(configs[i]); 6749e80f906SNeil Armstrong 6759e80f906SNeil Armstrong if (sx150x_pin_is_oscio(pctl, pin)) { 6769e80f906SNeil Armstrong if (param == PIN_CONFIG_OUTPUT) { 6779e80f906SNeil Armstrong ret = sx150x_gpio_direction_output(&pctl->gpio, 6789e80f906SNeil Armstrong pin, arg); 6799e80f906SNeil Armstrong if (ret < 0) 6809e80f906SNeil Armstrong return ret; 6819e80f906SNeil Armstrong 6829e80f906SNeil Armstrong continue; 6839e80f906SNeil Armstrong } else 6849e80f906SNeil Armstrong return -ENOTSUPP; 6859e80f906SNeil Armstrong } 6869e80f906SNeil Armstrong 6879e80f906SNeil Armstrong switch (param) { 6889e80f906SNeil Armstrong case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT: 6899e80f906SNeil Armstrong case PIN_CONFIG_BIAS_DISABLE: 6906489677fSAndrey Smirnov ret = regmap_write_bits(pctl->regmap, 6916489677fSAndrey Smirnov pctl->data->reg_pulldn, 6926489677fSAndrey Smirnov BIT(pin), 0); 6939e80f906SNeil Armstrong if (ret < 0) 6949e80f906SNeil Armstrong return ret; 6959e80f906SNeil Armstrong 6966489677fSAndrey Smirnov ret = regmap_write_bits(pctl->regmap, 6976489677fSAndrey Smirnov pctl->data->reg_pullup, 6986489677fSAndrey Smirnov BIT(pin), 0); 6999e80f906SNeil Armstrong if (ret < 0) 7009e80f906SNeil Armstrong return ret; 7019e80f906SNeil Armstrong 7029e80f906SNeil Armstrong break; 7039e80f906SNeil Armstrong 7049e80f906SNeil Armstrong case PIN_CONFIG_BIAS_PULL_UP: 7056489677fSAndrey Smirnov ret = regmap_write_bits(pctl->regmap, 7069e80f906SNeil Armstrong pctl->data->reg_pullup, 7076489677fSAndrey Smirnov BIT(pin), BIT(pin)); 7089e80f906SNeil Armstrong if (ret < 0) 7099e80f906SNeil Armstrong return ret; 7109e80f906SNeil Armstrong 7119e80f906SNeil Armstrong break; 7129e80f906SNeil Armstrong 7139e80f906SNeil Armstrong case PIN_CONFIG_BIAS_PULL_DOWN: 7146489677fSAndrey Smirnov ret = regmap_write_bits(pctl->regmap, 7159e80f906SNeil Armstrong pctl->data->reg_pulldn, 7166489677fSAndrey Smirnov BIT(pin), BIT(pin)); 7179e80f906SNeil Armstrong if (ret < 0) 7189e80f906SNeil Armstrong return ret; 7199e80f906SNeil Armstrong 7209e80f906SNeil Armstrong break; 7219e80f906SNeil Armstrong 7229e80f906SNeil Armstrong case PIN_CONFIG_DRIVE_OPEN_DRAIN: 7239e80f906SNeil Armstrong ret = sx150x_gpio_set_single_ended(&pctl->gpio, 7249e80f906SNeil Armstrong pin, LINE_MODE_OPEN_DRAIN); 7259e80f906SNeil Armstrong if (ret < 0) 7269e80f906SNeil Armstrong return ret; 7279e80f906SNeil Armstrong 7289e80f906SNeil Armstrong break; 7299e80f906SNeil Armstrong 7309e80f906SNeil Armstrong case PIN_CONFIG_DRIVE_PUSH_PULL: 7319e80f906SNeil Armstrong ret = sx150x_gpio_set_single_ended(&pctl->gpio, 7329e80f906SNeil Armstrong pin, LINE_MODE_PUSH_PULL); 7339e80f906SNeil Armstrong if (ret < 0) 7349e80f906SNeil Armstrong return ret; 7359e80f906SNeil Armstrong 7369e80f906SNeil Armstrong break; 7379e80f906SNeil Armstrong 7389e80f906SNeil Armstrong case PIN_CONFIG_OUTPUT: 7399e80f906SNeil Armstrong ret = sx150x_gpio_direction_output(&pctl->gpio, 7409e80f906SNeil Armstrong pin, arg); 7419e80f906SNeil Armstrong if (ret < 0) 7429e80f906SNeil Armstrong return ret; 7439e80f906SNeil Armstrong 7449e80f906SNeil Armstrong break; 7459e80f906SNeil Armstrong 7469e80f906SNeil Armstrong default: 7479e80f906SNeil Armstrong return -ENOTSUPP; 7489e80f906SNeil Armstrong } 7499e80f906SNeil Armstrong } /* for each config */ 7509e80f906SNeil Armstrong 7519e80f906SNeil Armstrong return 0; 7529e80f906SNeil Armstrong } 7539e80f906SNeil Armstrong 7549e80f906SNeil Armstrong static const struct pinconf_ops sx150x_pinconf_ops = { 7559e80f906SNeil Armstrong .pin_config_get = sx150x_pinconf_get, 7569e80f906SNeil Armstrong .pin_config_set = sx150x_pinconf_set, 7579e80f906SNeil Armstrong .is_generic = true, 7589e80f906SNeil Armstrong }; 7599e80f906SNeil Armstrong 7609e80f906SNeil Armstrong static const struct i2c_device_id sx150x_id[] = { 7619e80f906SNeil Armstrong {"sx1508q", (kernel_ulong_t) &sx1508q_device_data }, 7629e80f906SNeil Armstrong {"sx1509q", (kernel_ulong_t) &sx1509q_device_data }, 7639e80f906SNeil Armstrong {"sx1506q", (kernel_ulong_t) &sx1506q_device_data }, 7649e80f906SNeil Armstrong {"sx1502q", (kernel_ulong_t) &sx1502q_device_data }, 7656697546dSAndrey Smirnov {"sx1503q", (kernel_ulong_t) &sx1503q_device_data }, 7669e80f906SNeil Armstrong {} 7679e80f906SNeil Armstrong }; 7689e80f906SNeil Armstrong 7699e80f906SNeil Armstrong static const struct of_device_id sx150x_of_match[] = { 770e3ba8120SAndrey Smirnov { .compatible = "semtech,sx1508q", .data = &sx1508q_device_data }, 771e3ba8120SAndrey Smirnov { .compatible = "semtech,sx1509q", .data = &sx1509q_device_data }, 772e3ba8120SAndrey Smirnov { .compatible = "semtech,sx1506q", .data = &sx1506q_device_data }, 773e3ba8120SAndrey Smirnov { .compatible = "semtech,sx1502q", .data = &sx1502q_device_data }, 7746697546dSAndrey Smirnov { .compatible = "semtech,sx1503q", .data = &sx1503q_device_data }, 7759e80f906SNeil Armstrong {}, 7769e80f906SNeil Armstrong }; 7779e80f906SNeil Armstrong 7789e80f906SNeil Armstrong static int sx150x_reset(struct sx150x_pinctrl *pctl) 7799e80f906SNeil Armstrong { 7809e80f906SNeil Armstrong int err; 7819e80f906SNeil Armstrong 7829e80f906SNeil Armstrong err = i2c_smbus_write_byte_data(pctl->client, 7839e80f906SNeil Armstrong pctl->data->pri.x789.reg_reset, 784f9038d60SAndrey Smirnov SX150X_789_RESET_KEY1); 7859e80f906SNeil Armstrong if (err < 0) 7869e80f906SNeil Armstrong return err; 7879e80f906SNeil Armstrong 7889e80f906SNeil Armstrong err = i2c_smbus_write_byte_data(pctl->client, 7899e80f906SNeil Armstrong pctl->data->pri.x789.reg_reset, 790f9038d60SAndrey Smirnov SX150X_789_RESET_KEY2); 7919e80f906SNeil Armstrong return err; 7929e80f906SNeil Armstrong } 7939e80f906SNeil Armstrong 794310cdfa0SAndrey Smirnov static int sx150x_init_misc(struct sx150x_pinctrl *pctl) 795310cdfa0SAndrey Smirnov { 796310cdfa0SAndrey Smirnov u8 reg, value; 797310cdfa0SAndrey Smirnov 798310cdfa0SAndrey Smirnov switch (pctl->data->model) { 799310cdfa0SAndrey Smirnov case SX150X_789: 800310cdfa0SAndrey Smirnov reg = pctl->data->pri.x789.reg_misc; 801310cdfa0SAndrey Smirnov value = SX150X_789_REG_MISC_AUTOCLEAR_OFF; 802310cdfa0SAndrey Smirnov break; 803310cdfa0SAndrey Smirnov case SX150X_456: 804310cdfa0SAndrey Smirnov reg = pctl->data->pri.x456.reg_advance; 805310cdfa0SAndrey Smirnov value = 0x00; 806b30d31e4SAndrey Smirnov 807b30d31e4SAndrey Smirnov /* 808b30d31e4SAndrey Smirnov * Only SX1506 has RegAdvanced, SX1504/5 are expected 809b30d31e4SAndrey Smirnov * to initialize this offset to zero 810b30d31e4SAndrey Smirnov */ 811b30d31e4SAndrey Smirnov if (!reg) 812b30d31e4SAndrey Smirnov return 0; 813310cdfa0SAndrey Smirnov break; 814310cdfa0SAndrey Smirnov case SX150X_123: 815310cdfa0SAndrey Smirnov reg = pctl->data->pri.x123.reg_advance; 816310cdfa0SAndrey Smirnov value = 0x00; 817310cdfa0SAndrey Smirnov break; 818310cdfa0SAndrey Smirnov default: 819310cdfa0SAndrey Smirnov WARN(1, "Unknown chip model %d\n", pctl->data->model); 820310cdfa0SAndrey Smirnov return -EINVAL; 821310cdfa0SAndrey Smirnov } 822310cdfa0SAndrey Smirnov 8236489677fSAndrey Smirnov return regmap_write(pctl->regmap, reg, value); 824310cdfa0SAndrey Smirnov } 825310cdfa0SAndrey Smirnov 8269e80f906SNeil Armstrong static int sx150x_init_hw(struct sx150x_pinctrl *pctl) 8279e80f906SNeil Armstrong { 8286489677fSAndrey Smirnov const u8 reg[] = { 8296489677fSAndrey Smirnov [SX150X_789] = pctl->data->pri.x789.reg_polarity, 8306489677fSAndrey Smirnov [SX150X_456] = pctl->data->pri.x456.reg_pld_mode, 8316489677fSAndrey Smirnov [SX150X_123] = pctl->data->pri.x123.reg_pld_mode, 8326489677fSAndrey Smirnov }; 8339e80f906SNeil Armstrong int err; 8349e80f906SNeil Armstrong 8359e80f906SNeil Armstrong if (pctl->data->model == SX150X_789 && 8369e80f906SNeil Armstrong of_property_read_bool(pctl->dev->of_node, "semtech,probe-reset")) { 8379e80f906SNeil Armstrong err = sx150x_reset(pctl); 8389e80f906SNeil Armstrong if (err < 0) 8399e80f906SNeil Armstrong return err; 8409e80f906SNeil Armstrong } 8419e80f906SNeil Armstrong 842310cdfa0SAndrey Smirnov err = sx150x_init_misc(pctl); 8439e80f906SNeil Armstrong if (err < 0) 8449e80f906SNeil Armstrong return err; 8459e80f906SNeil Armstrong 8469e80f906SNeil Armstrong /* Set all pins to work in normal mode */ 8476489677fSAndrey Smirnov return regmap_write(pctl->regmap, reg[pctl->data->model], 0); 8489e80f906SNeil Armstrong } 8499e80f906SNeil Armstrong 8506489677fSAndrey Smirnov static int sx150x_regmap_reg_width(struct sx150x_pinctrl *pctl, 8516489677fSAndrey Smirnov unsigned int reg) 8526489677fSAndrey Smirnov { 8536489677fSAndrey Smirnov const struct sx150x_device_data *data = pctl->data; 8546489677fSAndrey Smirnov 8556489677fSAndrey Smirnov if (reg == data->reg_sense) { 8566489677fSAndrey Smirnov /* 8576489677fSAndrey Smirnov * RegSense packs two bits of configuration per GPIO, 8586489677fSAndrey Smirnov * so we'd need to read twice as many bits as there 8596489677fSAndrey Smirnov * are GPIO in our chip 8606489677fSAndrey Smirnov */ 8616489677fSAndrey Smirnov return 2 * data->ngpios; 8626489677fSAndrey Smirnov } else if ((data->model == SX150X_789 && 8636489677fSAndrey Smirnov (reg == data->pri.x789.reg_misc || 8646489677fSAndrey Smirnov reg == data->pri.x789.reg_clock || 8656489677fSAndrey Smirnov reg == data->pri.x789.reg_reset)) 8666489677fSAndrey Smirnov || 8676489677fSAndrey Smirnov (data->model == SX150X_123 && 8686489677fSAndrey Smirnov reg == data->pri.x123.reg_advance) 8696489677fSAndrey Smirnov || 8706489677fSAndrey Smirnov (data->model == SX150X_456 && 8716489677fSAndrey Smirnov reg == data->pri.x456.reg_advance)) { 8726489677fSAndrey Smirnov return 8; 8736489677fSAndrey Smirnov } else { 8746489677fSAndrey Smirnov return data->ngpios; 8756489677fSAndrey Smirnov } 8766489677fSAndrey Smirnov } 8776489677fSAndrey Smirnov 8786489677fSAndrey Smirnov static unsigned int sx150x_maybe_swizzle(struct sx150x_pinctrl *pctl, 8796489677fSAndrey Smirnov unsigned int reg, unsigned int val) 8806489677fSAndrey Smirnov { 8816489677fSAndrey Smirnov unsigned int a, b; 8826489677fSAndrey Smirnov const struct sx150x_device_data *data = pctl->data; 8836489677fSAndrey Smirnov 8846489677fSAndrey Smirnov /* 8856489677fSAndrey Smirnov * Whereas SX1509 presents RegSense in a simple layout as such: 8866489677fSAndrey Smirnov * reg [ f f e e d d c c ] 8876489677fSAndrey Smirnov * reg + 1 [ b b a a 9 9 8 8 ] 8886489677fSAndrey Smirnov * reg + 2 [ 7 7 6 6 5 5 4 4 ] 8896489677fSAndrey Smirnov * reg + 3 [ 3 3 2 2 1 1 0 0 ] 8906489677fSAndrey Smirnov * 8916489677fSAndrey Smirnov * SX1503 and SX1506 deviate from that data layout, instead storing 8927bd47496SPeter Rosin * their contents as follows: 8936489677fSAndrey Smirnov * 8946489677fSAndrey Smirnov * reg [ f f e e d d c c ] 8956489677fSAndrey Smirnov * reg + 1 [ 7 7 6 6 5 5 4 4 ] 8966489677fSAndrey Smirnov * reg + 2 [ b b a a 9 9 8 8 ] 8976489677fSAndrey Smirnov * reg + 3 [ 3 3 2 2 1 1 0 0 ] 8986489677fSAndrey Smirnov * 8996489677fSAndrey Smirnov * so, taking that into account, we swap two 9006489677fSAndrey Smirnov * inner bytes of a 4-byte result 9016489677fSAndrey Smirnov */ 9026489677fSAndrey Smirnov 9036489677fSAndrey Smirnov if (reg == data->reg_sense && 9046489677fSAndrey Smirnov data->ngpios == 16 && 9056489677fSAndrey Smirnov (data->model == SX150X_123 || 9066489677fSAndrey Smirnov data->model == SX150X_456)) { 9076489677fSAndrey Smirnov a = val & 0x00ff0000; 9086489677fSAndrey Smirnov b = val & 0x0000ff00; 9096489677fSAndrey Smirnov 9106489677fSAndrey Smirnov val &= 0xff0000ff; 9116489677fSAndrey Smirnov val |= b << 8; 9126489677fSAndrey Smirnov val |= a >> 8; 9136489677fSAndrey Smirnov } 9146489677fSAndrey Smirnov 9156489677fSAndrey Smirnov return val; 9166489677fSAndrey Smirnov } 9176489677fSAndrey Smirnov 9186489677fSAndrey Smirnov /* 9196489677fSAndrey Smirnov * In order to mask the differences between 16 and 8 bit expander 9206489677fSAndrey Smirnov * devices we set up a sligthly ficticious regmap that pretends to be 9216489677fSAndrey Smirnov * a set of 32-bit (to accomodate RegSenseLow/RegSenseHigh 9226489677fSAndrey Smirnov * pair/quartet) registers and transparently reconstructs those 9236489677fSAndrey Smirnov * registers via multiple I2C/SMBus reads 9246489677fSAndrey Smirnov * 9256489677fSAndrey Smirnov * This way the rest of the driver code, interfacing with the chip via 9266489677fSAndrey Smirnov * regmap API, can work assuming that each GPIO pin is represented by 9277bd47496SPeter Rosin * a group of bits at an offset proportional to GPIO number within a 9286489677fSAndrey Smirnov * given register. 9296489677fSAndrey Smirnov */ 9306489677fSAndrey Smirnov static int sx150x_regmap_reg_read(void *context, unsigned int reg, 9316489677fSAndrey Smirnov unsigned int *result) 9326489677fSAndrey Smirnov { 9336489677fSAndrey Smirnov int ret, n; 9346489677fSAndrey Smirnov struct sx150x_pinctrl *pctl = context; 9356489677fSAndrey Smirnov struct i2c_client *i2c = pctl->client; 9366489677fSAndrey Smirnov const int width = sx150x_regmap_reg_width(pctl, reg); 9376489677fSAndrey Smirnov unsigned int idx, val; 9386489677fSAndrey Smirnov 9396489677fSAndrey Smirnov /* 9407bd47496SPeter Rosin * There are four potential cases covered by this function: 9416489677fSAndrey Smirnov * 9426489677fSAndrey Smirnov * 1) 8-pin chip, single configuration bit register 9436489677fSAndrey Smirnov * 9446489677fSAndrey Smirnov * This is trivial the code below just needs to read: 9456489677fSAndrey Smirnov * reg [ 7 6 5 4 3 2 1 0 ] 9466489677fSAndrey Smirnov * 9476489677fSAndrey Smirnov * 2) 8-pin chip, double configuration bit register (RegSense) 9486489677fSAndrey Smirnov * 9496489677fSAndrey Smirnov * The read will be done as follows: 9506489677fSAndrey Smirnov * reg [ 7 7 6 6 5 5 4 4 ] 9516489677fSAndrey Smirnov * reg + 1 [ 3 3 2 2 1 1 0 0 ] 9526489677fSAndrey Smirnov * 9536489677fSAndrey Smirnov * 3) 16-pin chip, single configuration bit register 9546489677fSAndrey Smirnov * 9556489677fSAndrey Smirnov * The read will be done as follows: 9566489677fSAndrey Smirnov * reg [ f e d c b a 9 8 ] 9576489677fSAndrey Smirnov * reg + 1 [ 7 6 5 4 3 2 1 0 ] 9586489677fSAndrey Smirnov * 9596489677fSAndrey Smirnov * 4) 16-pin chip, double configuration bit register (RegSense) 9606489677fSAndrey Smirnov * 9616489677fSAndrey Smirnov * The read will be done as follows: 9626489677fSAndrey Smirnov * reg [ f f e e d d c c ] 9636489677fSAndrey Smirnov * reg + 1 [ b b a a 9 9 8 8 ] 9646489677fSAndrey Smirnov * reg + 2 [ 7 7 6 6 5 5 4 4 ] 9656489677fSAndrey Smirnov * reg + 3 [ 3 3 2 2 1 1 0 0 ] 9666489677fSAndrey Smirnov */ 9676489677fSAndrey Smirnov 9686489677fSAndrey Smirnov for (n = width, val = 0, idx = reg; n > 0; n -= 8, idx++) { 9696489677fSAndrey Smirnov val <<= 8; 9706489677fSAndrey Smirnov 9716489677fSAndrey Smirnov ret = i2c_smbus_read_byte_data(i2c, idx); 9726489677fSAndrey Smirnov if (ret < 0) 9736489677fSAndrey Smirnov return ret; 9746489677fSAndrey Smirnov 9756489677fSAndrey Smirnov val |= ret; 9766489677fSAndrey Smirnov } 9776489677fSAndrey Smirnov 9786489677fSAndrey Smirnov *result = sx150x_maybe_swizzle(pctl, reg, val); 9796489677fSAndrey Smirnov 9806489677fSAndrey Smirnov return 0; 9816489677fSAndrey Smirnov } 9826489677fSAndrey Smirnov 9836489677fSAndrey Smirnov static int sx150x_regmap_reg_write(void *context, unsigned int reg, 9846489677fSAndrey Smirnov unsigned int val) 9856489677fSAndrey Smirnov { 9866489677fSAndrey Smirnov int ret, n; 9876489677fSAndrey Smirnov struct sx150x_pinctrl *pctl = context; 9886489677fSAndrey Smirnov struct i2c_client *i2c = pctl->client; 9896489677fSAndrey Smirnov const int width = sx150x_regmap_reg_width(pctl, reg); 9906489677fSAndrey Smirnov 9916489677fSAndrey Smirnov val = sx150x_maybe_swizzle(pctl, reg, val); 9926489677fSAndrey Smirnov 9936489677fSAndrey Smirnov n = width - 8; 9946489677fSAndrey Smirnov do { 9956489677fSAndrey Smirnov const u8 byte = (val >> n) & 0xff; 9966489677fSAndrey Smirnov 9976489677fSAndrey Smirnov ret = i2c_smbus_write_byte_data(i2c, reg, byte); 9986489677fSAndrey Smirnov if (ret < 0) 9996489677fSAndrey Smirnov return ret; 10006489677fSAndrey Smirnov 10016489677fSAndrey Smirnov reg++; 10026489677fSAndrey Smirnov n -= 8; 10036489677fSAndrey Smirnov } while (n >= 0); 10046489677fSAndrey Smirnov 10059e80f906SNeil Armstrong return 0; 10069e80f906SNeil Armstrong } 10079e80f906SNeil Armstrong 10080db0f26cSAndrey Smirnov static bool sx150x_reg_volatile(struct device *dev, unsigned int reg) 10090db0f26cSAndrey Smirnov { 10100db0f26cSAndrey Smirnov struct sx150x_pinctrl *pctl = i2c_get_clientdata(to_i2c_client(dev)); 10110db0f26cSAndrey Smirnov 10126489677fSAndrey Smirnov return reg == pctl->data->reg_irq_src || reg == pctl->data->reg_data; 10130db0f26cSAndrey Smirnov } 10140db0f26cSAndrey Smirnov 10150db0f26cSAndrey Smirnov const struct regmap_config sx150x_regmap_config = { 10160db0f26cSAndrey Smirnov .reg_bits = 8, 10176489677fSAndrey Smirnov .val_bits = 32, 10180db0f26cSAndrey Smirnov 10190db0f26cSAndrey Smirnov .cache_type = REGCACHE_RBTREE, 10200db0f26cSAndrey Smirnov 10216489677fSAndrey Smirnov .reg_read = sx150x_regmap_reg_read, 10226489677fSAndrey Smirnov .reg_write = sx150x_regmap_reg_write, 10236489677fSAndrey Smirnov 10240db0f26cSAndrey Smirnov .max_register = SX150X_MAX_REGISTER, 10250db0f26cSAndrey Smirnov .volatile_reg = sx150x_reg_volatile, 10260db0f26cSAndrey Smirnov }; 10270db0f26cSAndrey Smirnov 10289e80f906SNeil Armstrong static int sx150x_probe(struct i2c_client *client, 10299e80f906SNeil Armstrong const struct i2c_device_id *id) 10309e80f906SNeil Armstrong { 10319e80f906SNeil Armstrong static const u32 i2c_funcs = I2C_FUNC_SMBUS_BYTE_DATA | 10329e80f906SNeil Armstrong I2C_FUNC_SMBUS_WRITE_WORD_DATA; 10339e80f906SNeil Armstrong struct device *dev = &client->dev; 10349e80f906SNeil Armstrong struct sx150x_pinctrl *pctl; 10359e80f906SNeil Armstrong int ret; 10369e80f906SNeil Armstrong 10379e80f906SNeil Armstrong if (!i2c_check_functionality(client->adapter, i2c_funcs)) 10389e80f906SNeil Armstrong return -ENOSYS; 10399e80f906SNeil Armstrong 10409e80f906SNeil Armstrong pctl = devm_kzalloc(dev, sizeof(*pctl), GFP_KERNEL); 10419e80f906SNeil Armstrong if (!pctl) 10429e80f906SNeil Armstrong return -ENOMEM; 10439e80f906SNeil Armstrong 10440db0f26cSAndrey Smirnov i2c_set_clientdata(client, pctl); 10450db0f26cSAndrey Smirnov 10469e80f906SNeil Armstrong pctl->dev = dev; 10479e80f906SNeil Armstrong pctl->client = client; 1048e3ba8120SAndrey Smirnov 1049e3ba8120SAndrey Smirnov if (dev->of_node) 1050e3ba8120SAndrey Smirnov pctl->data = of_device_get_match_data(dev); 1051e3ba8120SAndrey Smirnov else 1052e3ba8120SAndrey Smirnov pctl->data = (struct sx150x_device_data *)id->driver_data; 1053e3ba8120SAndrey Smirnov 1054e3ba8120SAndrey Smirnov if (!pctl->data) 1055e3ba8120SAndrey Smirnov return -EINVAL; 10569e80f906SNeil Armstrong 10576489677fSAndrey Smirnov pctl->regmap = devm_regmap_init(dev, NULL, pctl, 10586489677fSAndrey Smirnov &sx150x_regmap_config); 10590db0f26cSAndrey Smirnov if (IS_ERR(pctl->regmap)) { 10600db0f26cSAndrey Smirnov ret = PTR_ERR(pctl->regmap); 10610db0f26cSAndrey Smirnov dev_err(dev, "Failed to allocate register map: %d\n", 10620db0f26cSAndrey Smirnov ret); 10630db0f26cSAndrey Smirnov return ret; 10640db0f26cSAndrey Smirnov } 10650db0f26cSAndrey Smirnov 10669e80f906SNeil Armstrong mutex_init(&pctl->lock); 10679e80f906SNeil Armstrong 10689e80f906SNeil Armstrong ret = sx150x_init_hw(pctl); 10699e80f906SNeil Armstrong if (ret) 10709e80f906SNeil Armstrong return ret; 10719e80f906SNeil Armstrong 10729e80f906SNeil Armstrong /* Register GPIO controller */ 10739e80f906SNeil Armstrong pctl->gpio.label = devm_kstrdup(dev, client->name, GFP_KERNEL); 10749e80f906SNeil Armstrong pctl->gpio.base = -1; 10759e80f906SNeil Armstrong pctl->gpio.ngpio = pctl->data->npins; 10769e80f906SNeil Armstrong pctl->gpio.get_direction = sx150x_gpio_get_direction; 10779e80f906SNeil Armstrong pctl->gpio.direction_input = sx150x_gpio_direction_input; 10789e80f906SNeil Armstrong pctl->gpio.direction_output = sx150x_gpio_direction_output; 10799e80f906SNeil Armstrong pctl->gpio.get = sx150x_gpio_get; 10809e80f906SNeil Armstrong pctl->gpio.set = sx150x_gpio_set; 10819e80f906SNeil Armstrong pctl->gpio.set_single_ended = sx150x_gpio_set_single_ended; 10829e80f906SNeil Armstrong pctl->gpio.parent = dev; 10839e80f906SNeil Armstrong #ifdef CONFIG_OF_GPIO 10849e80f906SNeil Armstrong pctl->gpio.of_node = dev->of_node; 10859e80f906SNeil Armstrong #endif 10869e80f906SNeil Armstrong pctl->gpio.can_sleep = true; 1087*ec61168bSPeter Rosin /* 1088*ec61168bSPeter Rosin * Setting multiple pins is not safe when all pins are not 1089*ec61168bSPeter Rosin * handled by the same regmap register. The oscio pin (present 1090*ec61168bSPeter Rosin * on the SX150X_789 chips) lives in its own register, so 1091*ec61168bSPeter Rosin * would require locking that is not in place at this time. 1092*ec61168bSPeter Rosin */ 1093*ec61168bSPeter Rosin if (pctl->data->model != SX150X_789) 1094*ec61168bSPeter Rosin pctl->gpio.set_multiple = sx150x_gpio_set_multiple; 10959e80f906SNeil Armstrong 10969e80f906SNeil Armstrong ret = devm_gpiochip_add_data(dev, &pctl->gpio, pctl); 10979e80f906SNeil Armstrong if (ret) 10989e80f906SNeil Armstrong return ret; 10999e80f906SNeil Armstrong 11009e80f906SNeil Armstrong /* Add Interrupt support if an irq is specified */ 11019e80f906SNeil Armstrong if (client->irq > 0) { 11029e80f906SNeil Armstrong pctl->irq_chip.name = devm_kstrdup(dev, client->name, 11039e80f906SNeil Armstrong GFP_KERNEL); 11049e80f906SNeil Armstrong pctl->irq_chip.irq_mask = sx150x_irq_mask; 11059e80f906SNeil Armstrong pctl->irq_chip.irq_unmask = sx150x_irq_unmask; 11069e80f906SNeil Armstrong pctl->irq_chip.irq_set_type = sx150x_irq_set_type; 11079e80f906SNeil Armstrong pctl->irq_chip.irq_bus_lock = sx150x_irq_bus_lock; 11089e80f906SNeil Armstrong pctl->irq_chip.irq_bus_sync_unlock = sx150x_irq_bus_sync_unlock; 11099e80f906SNeil Armstrong 11109e80f906SNeil Armstrong pctl->irq.masked = ~0; 11119e80f906SNeil Armstrong pctl->irq.sense = 0; 11129e80f906SNeil Armstrong 1113080c489dSAndrey Smirnov /* 1114080c489dSAndrey Smirnov * Because sx150x_irq_threaded_fn invokes all of the 1115080c489dSAndrey Smirnov * nested interrrupt handlers via handle_nested_irq, 1116080c489dSAndrey Smirnov * any "handler" passed to gpiochip_irqchip_add() 1117080c489dSAndrey Smirnov * below is going to be ignored, so the choice of the 1118080c489dSAndrey Smirnov * function does not matter that much. 1119080c489dSAndrey Smirnov * 1120080c489dSAndrey Smirnov * We set it to handle_bad_irq to avoid confusion, 1121080c489dSAndrey Smirnov * plus it will be instantly noticeable if it is ever 1122080c489dSAndrey Smirnov * called (should not happen) 1123080c489dSAndrey Smirnov */ 11249e80f906SNeil Armstrong ret = gpiochip_irqchip_add(&pctl->gpio, 11259e80f906SNeil Armstrong &pctl->irq_chip, 0, 1126080c489dSAndrey Smirnov handle_bad_irq, IRQ_TYPE_NONE); 11279e80f906SNeil Armstrong if (ret) { 11289e80f906SNeil Armstrong dev_err(dev, "could not connect irqchip to gpiochip\n"); 11299e80f906SNeil Armstrong return ret; 11309e80f906SNeil Armstrong } 11319e80f906SNeil Armstrong 11329e80f906SNeil Armstrong ret = devm_request_threaded_irq(dev, client->irq, NULL, 11339e80f906SNeil Armstrong sx150x_irq_thread_fn, 11349e80f906SNeil Armstrong IRQF_ONESHOT | IRQF_SHARED | 11359e80f906SNeil Armstrong IRQF_TRIGGER_FALLING, 11369e80f906SNeil Armstrong pctl->irq_chip.name, pctl); 11379e80f906SNeil Armstrong if (ret < 0) 11389e80f906SNeil Armstrong return ret; 11399e80f906SNeil Armstrong } 11409e80f906SNeil Armstrong 11419e80f906SNeil Armstrong /* Pinctrl_desc */ 11429e80f906SNeil Armstrong pctl->pinctrl_desc.name = "sx150x-pinctrl"; 11439e80f906SNeil Armstrong pctl->pinctrl_desc.pctlops = &sx150x_pinctrl_ops; 11449e80f906SNeil Armstrong pctl->pinctrl_desc.confops = &sx150x_pinconf_ops; 11459e80f906SNeil Armstrong pctl->pinctrl_desc.pins = pctl->data->pins; 11469e80f906SNeil Armstrong pctl->pinctrl_desc.npins = pctl->data->npins; 11479e80f906SNeil Armstrong pctl->pinctrl_desc.owner = THIS_MODULE; 11489e80f906SNeil Armstrong 11499e80f906SNeil Armstrong pctl->pctldev = pinctrl_register(&pctl->pinctrl_desc, dev, pctl); 11509e80f906SNeil Armstrong if (IS_ERR(pctl->pctldev)) { 11519e80f906SNeil Armstrong dev_err(dev, "Failed to register pinctrl device\n"); 11529e80f906SNeil Armstrong return PTR_ERR(pctl->pctldev); 11539e80f906SNeil Armstrong } 11549e80f906SNeil Armstrong 11559e80f906SNeil Armstrong return 0; 11569e80f906SNeil Armstrong } 11579e80f906SNeil Armstrong 11589e80f906SNeil Armstrong static struct i2c_driver sx150x_driver = { 11599e80f906SNeil Armstrong .driver = { 11609e80f906SNeil Armstrong .name = "sx150x-pinctrl", 11619e80f906SNeil Armstrong .of_match_table = of_match_ptr(sx150x_of_match), 11629e80f906SNeil Armstrong }, 11639e80f906SNeil Armstrong .probe = sx150x_probe, 11649e80f906SNeil Armstrong .id_table = sx150x_id, 11659e80f906SNeil Armstrong }; 11669e80f906SNeil Armstrong 11679e80f906SNeil Armstrong static int __init sx150x_init(void) 11689e80f906SNeil Armstrong { 11699e80f906SNeil Armstrong return i2c_add_driver(&sx150x_driver); 11709e80f906SNeil Armstrong } 11719e80f906SNeil Armstrong subsys_initcall(sx150x_init); 1172