1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2016, BayLibre, SAS. All rights reserved.
4 * Author: Neil Armstrong <narmstrong@baylibre.com>
6 * Copyright (c) 2010, Code Aurora Forum. All rights reserved.
8 * Driver for Semtech SX150X I2C GPIO Expanders
9 * The handling of the 4-bit chips (SX1501/SX1504/SX1507) is untested.
11 * Author: Gregory Bean <gbean@codeaurora.org>
14 #include <linux/regmap.h>
15 #include <linux/i2c.h>
16 #include <linux/init.h>
17 #include <linux/interrupt.h>
18 #include <linux/irq.h>
19 #include <linux/mutex.h>
20 #include <linux/slab.h>
22 #include <linux/gpio/driver.h>
23 #include <linux/pinctrl/pinconf.h>
24 #include <linux/pinctrl/pinctrl.h>
25 #include <linux/pinctrl/pinmux.h>
26 #include <linux/pinctrl/pinconf-generic.h>
30 #include "pinctrl-utils.h"
32 /* The chip models of sx150x */
39 SX150X_789_REG_MISC_AUTOCLEAR_OFF
= 1 << 0,
40 SX150X_MAX_REGISTER
= 0xad,
41 SX150X_IRQ_TYPE_EDGE_RISING
= 0x1,
42 SX150X_IRQ_TYPE_EDGE_FALLING
= 0x2,
43 SX150X_789_RESET_KEY1
= 0x12,
44 SX150X_789_RESET_KEY2
= 0x34,
47 struct sx150x_123_pri
{
57 struct sx150x_456_pri
{
67 struct sx150x_789_pri
{
76 struct sx150x_device_data
{
87 struct sx150x_123_pri x123
;
88 struct sx150x_456_pri x456
;
89 struct sx150x_789_pri x789
;
91 const struct pinctrl_pin_desc
*pins
;
95 struct sx150x_pinctrl
{
97 struct i2c_client
*client
;
98 struct pinctrl_dev
*pctldev
;
99 struct pinctrl_desc pinctrl_desc
;
100 struct gpio_chip gpio
;
101 struct regmap
*regmap
;
107 const struct sx150x_device_data
*data
;
110 static const struct pinctrl_pin_desc sx150x_4_pins
[] = {
111 PINCTRL_PIN(0, "gpio0"),
112 PINCTRL_PIN(1, "gpio1"),
113 PINCTRL_PIN(2, "gpio2"),
114 PINCTRL_PIN(3, "gpio3"),
115 PINCTRL_PIN(4, "oscio"),
118 static const struct pinctrl_pin_desc sx150x_8_pins
[] = {
119 PINCTRL_PIN(0, "gpio0"),
120 PINCTRL_PIN(1, "gpio1"),
121 PINCTRL_PIN(2, "gpio2"),
122 PINCTRL_PIN(3, "gpio3"),
123 PINCTRL_PIN(4, "gpio4"),
124 PINCTRL_PIN(5, "gpio5"),
125 PINCTRL_PIN(6, "gpio6"),
126 PINCTRL_PIN(7, "gpio7"),
127 PINCTRL_PIN(8, "oscio"),
130 static const struct pinctrl_pin_desc sx150x_16_pins
[] = {
131 PINCTRL_PIN(0, "gpio0"),
132 PINCTRL_PIN(1, "gpio1"),
133 PINCTRL_PIN(2, "gpio2"),
134 PINCTRL_PIN(3, "gpio3"),
135 PINCTRL_PIN(4, "gpio4"),
136 PINCTRL_PIN(5, "gpio5"),
137 PINCTRL_PIN(6, "gpio6"),
138 PINCTRL_PIN(7, "gpio7"),
139 PINCTRL_PIN(8, "gpio8"),
140 PINCTRL_PIN(9, "gpio9"),
141 PINCTRL_PIN(10, "gpio10"),
142 PINCTRL_PIN(11, "gpio11"),
143 PINCTRL_PIN(12, "gpio12"),
144 PINCTRL_PIN(13, "gpio13"),
145 PINCTRL_PIN(14, "gpio14"),
146 PINCTRL_PIN(15, "gpio15"),
147 PINCTRL_PIN(16, "oscio"),
150 static const struct sx150x_device_data sx1501q_device_data
= {
156 .reg_irq_mask
= 0x05,
160 .reg_pld_mode
= 0x10,
161 .reg_pld_table0
= 0x11,
162 .reg_pld_table2
= 0x13,
163 .reg_advanced
= 0xad,
166 .pins
= sx150x_4_pins
,
167 .npins
= 4, /* oscio not available */
170 static const struct sx150x_device_data sx1502q_device_data
= {
176 .reg_irq_mask
= 0x05,
180 .reg_pld_mode
= 0x10,
181 .reg_pld_table0
= 0x11,
182 .reg_pld_table1
= 0x12,
183 .reg_pld_table2
= 0x13,
184 .reg_pld_table3
= 0x14,
185 .reg_pld_table4
= 0x15,
186 .reg_advanced
= 0xad,
189 .pins
= sx150x_8_pins
,
190 .npins
= 8, /* oscio not available */
193 static const struct sx150x_device_data sx1503q_device_data
= {
199 .reg_irq_mask
= 0x08,
203 .reg_pld_mode
= 0x20,
204 .reg_pld_table0
= 0x22,
205 .reg_pld_table1
= 0x24,
206 .reg_pld_table2
= 0x26,
207 .reg_pld_table3
= 0x28,
208 .reg_pld_table4
= 0x2a,
209 .reg_advanced
= 0xad,
212 .pins
= sx150x_16_pins
,
213 .npins
= 16, /* oscio not available */
216 static const struct sx150x_device_data sx1504q_device_data
= {
222 .reg_irq_mask
= 0x05,
226 .reg_pld_mode
= 0x10,
227 .reg_pld_table0
= 0x11,
228 .reg_pld_table2
= 0x13,
231 .pins
= sx150x_4_pins
,
232 .npins
= 4, /* oscio not available */
235 static const struct sx150x_device_data sx1505q_device_data
= {
241 .reg_irq_mask
= 0x05,
245 .reg_pld_mode
= 0x10,
246 .reg_pld_table0
= 0x11,
247 .reg_pld_table1
= 0x12,
248 .reg_pld_table2
= 0x13,
249 .reg_pld_table3
= 0x14,
250 .reg_pld_table4
= 0x15,
253 .pins
= sx150x_8_pins
,
254 .npins
= 8, /* oscio not available */
257 static const struct sx150x_device_data sx1506q_device_data
= {
263 .reg_irq_mask
= 0x08,
267 .reg_pld_mode
= 0x20,
268 .reg_pld_table0
= 0x22,
269 .reg_pld_table1
= 0x24,
270 .reg_pld_table2
= 0x26,
271 .reg_pld_table3
= 0x28,
272 .reg_pld_table4
= 0x2a,
273 .reg_advanced
= 0xad,
276 .pins
= sx150x_16_pins
,
277 .npins
= 16, /* oscio not available */
280 static const struct sx150x_device_data sx1507q_device_data
= {
286 .reg_irq_mask
= 0x09,
291 .reg_polarity
= 0x06,
297 .pins
= sx150x_4_pins
,
298 .npins
= ARRAY_SIZE(sx150x_4_pins
),
301 static const struct sx150x_device_data sx1508q_device_data
= {
307 .reg_irq_mask
= 0x09,
312 .reg_polarity
= 0x06,
318 .pins
= sx150x_8_pins
,
319 .npins
= ARRAY_SIZE(sx150x_8_pins
),
322 static const struct sx150x_device_data sx1509q_device_data
= {
328 .reg_irq_mask
= 0x12,
333 .reg_polarity
= 0x0c,
339 .pins
= sx150x_16_pins
,
340 .npins
= ARRAY_SIZE(sx150x_16_pins
),
343 static int sx150x_pinctrl_get_groups_count(struct pinctrl_dev
*pctldev
)
348 static const char *sx150x_pinctrl_get_group_name(struct pinctrl_dev
*pctldev
,
354 static int sx150x_pinctrl_get_group_pins(struct pinctrl_dev
*pctldev
,
356 const unsigned int **pins
,
357 unsigned int *num_pins
)
362 static const struct pinctrl_ops sx150x_pinctrl_ops
= {
363 .get_groups_count
= sx150x_pinctrl_get_groups_count
,
364 .get_group_name
= sx150x_pinctrl_get_group_name
,
365 .get_group_pins
= sx150x_pinctrl_get_group_pins
,
367 .dt_node_to_map
= pinconf_generic_dt_node_to_map_pin
,
368 .dt_free_map
= pinctrl_utils_free_map
,
372 static bool sx150x_pin_is_oscio(struct sx150x_pinctrl
*pctl
, unsigned int pin
)
374 if (pin
>= pctl
->data
->npins
)
377 /* OSCIO pin is only present in 789 devices */
378 if (pctl
->data
->model
!= SX150X_789
)
381 return !strcmp(pctl
->data
->pins
[pin
].name
, "oscio");
384 static int sx150x_gpio_get_direction(struct gpio_chip
*chip
,
387 struct sx150x_pinctrl
*pctl
= gpiochip_get_data(chip
);
391 if (sx150x_pin_is_oscio(pctl
, offset
))
392 return GPIO_LINE_DIRECTION_OUT
;
394 ret
= regmap_read(pctl
->regmap
, pctl
->data
->reg_dir
, &value
);
398 if (value
& BIT(offset
))
399 return GPIO_LINE_DIRECTION_IN
;
401 return GPIO_LINE_DIRECTION_OUT
;
404 static int sx150x_gpio_get(struct gpio_chip
*chip
, unsigned int offset
)
406 struct sx150x_pinctrl
*pctl
= gpiochip_get_data(chip
);
410 if (sx150x_pin_is_oscio(pctl
, offset
))
413 ret
= regmap_read(pctl
->regmap
, pctl
->data
->reg_data
, &value
);
417 return !!(value
& BIT(offset
));
420 static int __sx150x_gpio_set(struct sx150x_pinctrl
*pctl
, unsigned int offset
,
423 return regmap_write_bits(pctl
->regmap
, pctl
->data
->reg_data
,
424 BIT(offset
), value
? BIT(offset
) : 0);
427 static int sx150x_gpio_oscio_set(struct sx150x_pinctrl
*pctl
,
430 return regmap_write(pctl
->regmap
,
431 pctl
->data
->pri
.x789
.reg_clock
,
432 (value
? 0x1f : 0x10));
435 static void sx150x_gpio_set(struct gpio_chip
*chip
, unsigned int offset
,
438 struct sx150x_pinctrl
*pctl
= gpiochip_get_data(chip
);
440 if (sx150x_pin_is_oscio(pctl
, offset
))
441 sx150x_gpio_oscio_set(pctl
, value
);
443 __sx150x_gpio_set(pctl
, offset
, value
);
446 static void sx150x_gpio_set_multiple(struct gpio_chip
*chip
,
450 struct sx150x_pinctrl
*pctl
= gpiochip_get_data(chip
);
452 regmap_write_bits(pctl
->regmap
, pctl
->data
->reg_data
, *mask
, *bits
);
455 static int sx150x_gpio_direction_input(struct gpio_chip
*chip
,
458 struct sx150x_pinctrl
*pctl
= gpiochip_get_data(chip
);
460 if (sx150x_pin_is_oscio(pctl
, offset
))
463 return regmap_write_bits(pctl
->regmap
,
465 BIT(offset
), BIT(offset
));
468 static int sx150x_gpio_direction_output(struct gpio_chip
*chip
,
469 unsigned int offset
, int value
)
471 struct sx150x_pinctrl
*pctl
= gpiochip_get_data(chip
);
474 if (sx150x_pin_is_oscio(pctl
, offset
))
475 return sx150x_gpio_oscio_set(pctl
, value
);
477 ret
= __sx150x_gpio_set(pctl
, offset
, value
);
481 return regmap_write_bits(pctl
->regmap
,
486 static void sx150x_irq_mask(struct irq_data
*d
)
488 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
489 struct sx150x_pinctrl
*pctl
= gpiochip_get_data(gc
);
490 unsigned int n
= irqd_to_hwirq(d
);
492 pctl
->irq
.masked
|= BIT(n
);
493 gpiochip_disable_irq(gc
, n
);
496 static void sx150x_irq_unmask(struct irq_data
*d
)
498 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
499 struct sx150x_pinctrl
*pctl
= gpiochip_get_data(gc
);
500 unsigned int n
= irqd_to_hwirq(d
);
502 gpiochip_enable_irq(gc
, n
);
503 pctl
->irq
.masked
&= ~BIT(n
);
506 static void sx150x_irq_set_sense(struct sx150x_pinctrl
*pctl
,
507 unsigned int line
, unsigned int sense
)
510 * Every interrupt line is represented by two bits shifted
511 * proportionally to the line number
513 const unsigned int n
= line
* 2;
514 const unsigned int mask
= ~((SX150X_IRQ_TYPE_EDGE_RISING
|
515 SX150X_IRQ_TYPE_EDGE_FALLING
) << n
);
517 pctl
->irq
.sense
&= mask
;
518 pctl
->irq
.sense
|= sense
<< n
;
521 static int sx150x_irq_set_type(struct irq_data
*d
, unsigned int flow_type
)
523 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
524 struct sx150x_pinctrl
*pctl
= gpiochip_get_data(gc
);
525 unsigned int n
, val
= 0;
527 if (flow_type
& (IRQ_TYPE_LEVEL_HIGH
| IRQ_TYPE_LEVEL_LOW
))
530 n
= irqd_to_hwirq(d
);
532 if (flow_type
& IRQ_TYPE_EDGE_RISING
)
533 val
|= SX150X_IRQ_TYPE_EDGE_RISING
;
534 if (flow_type
& IRQ_TYPE_EDGE_FALLING
)
535 val
|= SX150X_IRQ_TYPE_EDGE_FALLING
;
537 sx150x_irq_set_sense(pctl
, n
, val
);
541 static irqreturn_t
sx150x_irq_thread_fn(int irq
, void *dev_id
)
543 struct sx150x_pinctrl
*pctl
= (struct sx150x_pinctrl
*)dev_id
;
544 unsigned long n
, status
;
548 err
= regmap_read(pctl
->regmap
, pctl
->data
->reg_irq_src
, &val
);
552 err
= regmap_write(pctl
->regmap
, pctl
->data
->reg_irq_src
, val
);
557 for_each_set_bit(n
, &status
, pctl
->data
->ngpios
)
558 handle_nested_irq(irq_find_mapping(pctl
->gpio
.irq
.domain
, n
));
563 static void sx150x_irq_bus_lock(struct irq_data
*d
)
565 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
566 struct sx150x_pinctrl
*pctl
= gpiochip_get_data(gc
);
568 mutex_lock(&pctl
->lock
);
571 static void sx150x_irq_bus_sync_unlock(struct irq_data
*d
)
573 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
574 struct sx150x_pinctrl
*pctl
= gpiochip_get_data(gc
);
576 regmap_write(pctl
->regmap
, pctl
->data
->reg_irq_mask
, pctl
->irq
.masked
);
577 regmap_write(pctl
->regmap
, pctl
->data
->reg_sense
, pctl
->irq
.sense
);
578 mutex_unlock(&pctl
->lock
);
582 static void sx150x_irq_print_chip(struct irq_data
*d
, struct seq_file
*p
)
584 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
585 struct sx150x_pinctrl
*pctl
= gpiochip_get_data(gc
);
587 seq_puts(p
, pctl
->client
->name
);
590 static const struct irq_chip sx150x_irq_chip
= {
591 .irq_mask
= sx150x_irq_mask
,
592 .irq_unmask
= sx150x_irq_unmask
,
593 .irq_set_type
= sx150x_irq_set_type
,
594 .irq_bus_lock
= sx150x_irq_bus_lock
,
595 .irq_bus_sync_unlock
= sx150x_irq_bus_sync_unlock
,
596 .irq_print_chip
= sx150x_irq_print_chip
,
597 .flags
= IRQCHIP_IMMUTABLE
,
598 GPIOCHIP_IRQ_RESOURCE_HELPERS
,
601 static int sx150x_pinconf_get(struct pinctrl_dev
*pctldev
, unsigned int pin
,
602 unsigned long *config
)
604 struct sx150x_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
605 unsigned int param
= pinconf_to_config_param(*config
);
610 if (sx150x_pin_is_oscio(pctl
, pin
)) {
612 case PIN_CONFIG_DRIVE_PUSH_PULL
:
613 case PIN_CONFIG_OUTPUT
:
614 ret
= regmap_read(pctl
->regmap
,
615 pctl
->data
->pri
.x789
.reg_clock
,
620 if (param
== PIN_CONFIG_DRIVE_PUSH_PULL
)
621 arg
= (data
& 0x1f) ? 1 : 0;
623 if ((data
& 0x1f) == 0x1f)
625 else if ((data
& 0x1f) == 0x10)
640 case PIN_CONFIG_BIAS_PULL_DOWN
:
641 ret
= regmap_read(pctl
->regmap
,
642 pctl
->data
->reg_pulldn
,
655 case PIN_CONFIG_BIAS_PULL_UP
:
656 ret
= regmap_read(pctl
->regmap
,
657 pctl
->data
->reg_pullup
,
670 case PIN_CONFIG_DRIVE_OPEN_DRAIN
:
671 if (pctl
->data
->model
!= SX150X_789
)
674 ret
= regmap_read(pctl
->regmap
,
675 pctl
->data
->pri
.x789
.reg_drain
,
688 case PIN_CONFIG_DRIVE_PUSH_PULL
:
689 if (pctl
->data
->model
!= SX150X_789
)
692 ret
= regmap_read(pctl
->regmap
,
693 pctl
->data
->pri
.x789
.reg_drain
,
707 case PIN_CONFIG_OUTPUT
:
708 ret
= sx150x_gpio_get_direction(&pctl
->gpio
, pin
);
712 if (ret
== GPIO_LINE_DIRECTION_IN
)
715 ret
= sx150x_gpio_get(&pctl
->gpio
, pin
);
727 *config
= pinconf_to_config_packed(param
, arg
);
732 static int sx150x_pinconf_set(struct pinctrl_dev
*pctldev
, unsigned int pin
,
733 unsigned long *configs
, unsigned int num_configs
)
735 struct sx150x_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
736 enum pin_config_param param
;
741 for (i
= 0; i
< num_configs
; i
++) {
742 param
= pinconf_to_config_param(configs
[i
]);
743 arg
= pinconf_to_config_argument(configs
[i
]);
745 if (sx150x_pin_is_oscio(pctl
, pin
)) {
746 if (param
== PIN_CONFIG_OUTPUT
) {
747 ret
= sx150x_gpio_direction_output(&pctl
->gpio
,
758 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT
:
759 case PIN_CONFIG_BIAS_DISABLE
:
760 ret
= regmap_write_bits(pctl
->regmap
,
761 pctl
->data
->reg_pulldn
,
766 ret
= regmap_write_bits(pctl
->regmap
,
767 pctl
->data
->reg_pullup
,
774 case PIN_CONFIG_BIAS_PULL_UP
:
775 ret
= regmap_write_bits(pctl
->regmap
,
776 pctl
->data
->reg_pullup
,
783 case PIN_CONFIG_BIAS_PULL_DOWN
:
784 ret
= regmap_write_bits(pctl
->regmap
,
785 pctl
->data
->reg_pulldn
,
792 case PIN_CONFIG_DRIVE_OPEN_DRAIN
:
793 if (pctl
->data
->model
!= SX150X_789
||
794 sx150x_pin_is_oscio(pctl
, pin
))
797 ret
= regmap_write_bits(pctl
->regmap
,
798 pctl
->data
->pri
.x789
.reg_drain
,
805 case PIN_CONFIG_DRIVE_PUSH_PULL
:
806 if (pctl
->data
->model
!= SX150X_789
||
807 sx150x_pin_is_oscio(pctl
, pin
))
810 ret
= regmap_write_bits(pctl
->regmap
,
811 pctl
->data
->pri
.x789
.reg_drain
,
818 case PIN_CONFIG_OUTPUT
:
819 ret
= sx150x_gpio_direction_output(&pctl
->gpio
,
829 } /* for each config */
834 static const struct pinconf_ops sx150x_pinconf_ops
= {
835 .pin_config_get
= sx150x_pinconf_get
,
836 .pin_config_set
= sx150x_pinconf_set
,
840 static const struct i2c_device_id sx150x_id
[] = {
841 {"sx1501q", (kernel_ulong_t
) &sx1501q_device_data
},
842 {"sx1502q", (kernel_ulong_t
) &sx1502q_device_data
},
843 {"sx1503q", (kernel_ulong_t
) &sx1503q_device_data
},
844 {"sx1504q", (kernel_ulong_t
) &sx1504q_device_data
},
845 {"sx1505q", (kernel_ulong_t
) &sx1505q_device_data
},
846 {"sx1506q", (kernel_ulong_t
) &sx1506q_device_data
},
847 {"sx1507q", (kernel_ulong_t
) &sx1507q_device_data
},
848 {"sx1508q", (kernel_ulong_t
) &sx1508q_device_data
},
849 {"sx1509q", (kernel_ulong_t
) &sx1509q_device_data
},
853 static const struct of_device_id sx150x_of_match
[] = {
854 { .compatible
= "semtech,sx1501q", .data
= &sx1501q_device_data
},
855 { .compatible
= "semtech,sx1502q", .data
= &sx1502q_device_data
},
856 { .compatible
= "semtech,sx1503q", .data
= &sx1503q_device_data
},
857 { .compatible
= "semtech,sx1504q", .data
= &sx1504q_device_data
},
858 { .compatible
= "semtech,sx1505q", .data
= &sx1505q_device_data
},
859 { .compatible
= "semtech,sx1506q", .data
= &sx1506q_device_data
},
860 { .compatible
= "semtech,sx1507q", .data
= &sx1507q_device_data
},
861 { .compatible
= "semtech,sx1508q", .data
= &sx1508q_device_data
},
862 { .compatible
= "semtech,sx1509q", .data
= &sx1509q_device_data
},
866 static int sx150x_reset(struct sx150x_pinctrl
*pctl
)
870 err
= i2c_smbus_write_byte_data(pctl
->client
,
871 pctl
->data
->pri
.x789
.reg_reset
,
872 SX150X_789_RESET_KEY1
);
876 err
= i2c_smbus_write_byte_data(pctl
->client
,
877 pctl
->data
->pri
.x789
.reg_reset
,
878 SX150X_789_RESET_KEY2
);
882 static int sx150x_init_misc(struct sx150x_pinctrl
*pctl
)
886 switch (pctl
->data
->model
) {
888 reg
= pctl
->data
->pri
.x789
.reg_misc
;
889 value
= SX150X_789_REG_MISC_AUTOCLEAR_OFF
;
892 reg
= pctl
->data
->pri
.x456
.reg_advanced
;
896 * Only SX1506 has RegAdvanced, SX1504/5 are expected
897 * to initialize this offset to zero
903 reg
= pctl
->data
->pri
.x123
.reg_advanced
;
907 WARN(1, "Unknown chip model %d\n", pctl
->data
->model
);
911 return regmap_write(pctl
->regmap
, reg
, value
);
914 static int sx150x_init_hw(struct sx150x_pinctrl
*pctl
)
917 [SX150X_789
] = pctl
->data
->pri
.x789
.reg_polarity
,
918 [SX150X_456
] = pctl
->data
->pri
.x456
.reg_pld_mode
,
919 [SX150X_123
] = pctl
->data
->pri
.x123
.reg_pld_mode
,
923 if (pctl
->data
->model
== SX150X_789
&&
924 of_property_read_bool(pctl
->dev
->of_node
, "semtech,probe-reset")) {
925 err
= sx150x_reset(pctl
);
930 err
= sx150x_init_misc(pctl
);
934 /* Set all pins to work in normal mode */
935 return regmap_write(pctl
->regmap
, reg
[pctl
->data
->model
], 0);
938 static int sx150x_regmap_reg_width(struct sx150x_pinctrl
*pctl
,
941 const struct sx150x_device_data
*data
= pctl
->data
;
943 if (reg
== data
->reg_sense
) {
945 * RegSense packs two bits of configuration per GPIO,
946 * so we'd need to read twice as many bits as there
947 * are GPIO in our chip
949 return 2 * data
->ngpios
;
950 } else if ((data
->model
== SX150X_789
&&
951 (reg
== data
->pri
.x789
.reg_misc
||
952 reg
== data
->pri
.x789
.reg_clock
||
953 reg
== data
->pri
.x789
.reg_reset
))
955 (data
->model
== SX150X_123
&&
956 reg
== data
->pri
.x123
.reg_advanced
)
958 (data
->model
== SX150X_456
&&
959 data
->pri
.x456
.reg_advanced
&&
960 reg
== data
->pri
.x456
.reg_advanced
)) {
967 static unsigned int sx150x_maybe_swizzle(struct sx150x_pinctrl
*pctl
,
968 unsigned int reg
, unsigned int val
)
971 const struct sx150x_device_data
*data
= pctl
->data
;
974 * Whereas SX1509 presents RegSense in a simple layout as such:
975 * reg [ f f e e d d c c ]
976 * reg + 1 [ b b a a 9 9 8 8 ]
977 * reg + 2 [ 7 7 6 6 5 5 4 4 ]
978 * reg + 3 [ 3 3 2 2 1 1 0 0 ]
980 * SX1503 and SX1506 deviate from that data layout, instead storing
981 * their contents as follows:
983 * reg [ f f e e d d c c ]
984 * reg + 1 [ 7 7 6 6 5 5 4 4 ]
985 * reg + 2 [ b b a a 9 9 8 8 ]
986 * reg + 3 [ 3 3 2 2 1 1 0 0 ]
988 * so, taking that into account, we swap two
989 * inner bytes of a 4-byte result
992 if (reg
== data
->reg_sense
&&
993 data
->ngpios
== 16 &&
994 (data
->model
== SX150X_123
||
995 data
->model
== SX150X_456
)) {
996 a
= val
& 0x00ff0000;
997 b
= val
& 0x0000ff00;
1008 * In order to mask the differences between 16 and 8 bit expander
1009 * devices we set up a sligthly ficticious regmap that pretends to be
1010 * a set of 32-bit (to accommodate RegSenseLow/RegSenseHigh
1011 * pair/quartet) registers and transparently reconstructs those
1012 * registers via multiple I2C/SMBus reads
1014 * This way the rest of the driver code, interfacing with the chip via
1015 * regmap API, can work assuming that each GPIO pin is represented by
1016 * a group of bits at an offset proportional to GPIO number within a
1019 static int sx150x_regmap_reg_read(void *context
, unsigned int reg
,
1020 unsigned int *result
)
1023 struct sx150x_pinctrl
*pctl
= context
;
1024 struct i2c_client
*i2c
= pctl
->client
;
1025 const int width
= sx150x_regmap_reg_width(pctl
, reg
);
1026 unsigned int idx
, val
;
1029 * There are four potential cases covered by this function:
1031 * 1) 8-pin chip, single configuration bit register
1033 * This is trivial the code below just needs to read:
1034 * reg [ 7 6 5 4 3 2 1 0 ]
1036 * 2) 8-pin chip, double configuration bit register (RegSense)
1038 * The read will be done as follows:
1039 * reg [ 7 7 6 6 5 5 4 4 ]
1040 * reg + 1 [ 3 3 2 2 1 1 0 0 ]
1042 * 3) 16-pin chip, single configuration bit register
1044 * The read will be done as follows:
1045 * reg [ f e d c b a 9 8 ]
1046 * reg + 1 [ 7 6 5 4 3 2 1 0 ]
1048 * 4) 16-pin chip, double configuration bit register (RegSense)
1050 * The read will be done as follows:
1051 * reg [ f f e e d d c c ]
1052 * reg + 1 [ b b a a 9 9 8 8 ]
1053 * reg + 2 [ 7 7 6 6 5 5 4 4 ]
1054 * reg + 3 [ 3 3 2 2 1 1 0 0 ]
1057 for (n
= width
, val
= 0, idx
= reg
; n
> 0; n
-= 8, idx
++) {
1060 ret
= i2c_smbus_read_byte_data(i2c
, idx
);
1067 *result
= sx150x_maybe_swizzle(pctl
, reg
, val
);
1072 static int sx150x_regmap_reg_write(void *context
, unsigned int reg
,
1076 struct sx150x_pinctrl
*pctl
= context
;
1077 struct i2c_client
*i2c
= pctl
->client
;
1078 const int width
= sx150x_regmap_reg_width(pctl
, reg
);
1080 val
= sx150x_maybe_swizzle(pctl
, reg
, val
);
1082 n
= (width
- 1) & ~7;
1084 const u8 byte
= (val
>> n
) & 0xff;
1086 ret
= i2c_smbus_write_byte_data(i2c
, reg
, byte
);
1097 static bool sx150x_reg_volatile(struct device
*dev
, unsigned int reg
)
1099 struct sx150x_pinctrl
*pctl
= i2c_get_clientdata(to_i2c_client(dev
));
1101 return reg
== pctl
->data
->reg_irq_src
|| reg
== pctl
->data
->reg_data
;
1104 static const struct regmap_config sx150x_regmap_config
= {
1108 .cache_type
= REGCACHE_MAPLE
,
1110 .reg_read
= sx150x_regmap_reg_read
,
1111 .reg_write
= sx150x_regmap_reg_write
,
1113 .max_register
= SX150X_MAX_REGISTER
,
1114 .volatile_reg
= sx150x_reg_volatile
,
1117 static int sx150x_probe(struct i2c_client
*client
)
1119 static const u32 i2c_funcs
= I2C_FUNC_SMBUS_BYTE_DATA
|
1120 I2C_FUNC_SMBUS_WRITE_WORD_DATA
;
1121 struct device
*dev
= &client
->dev
;
1122 struct sx150x_pinctrl
*pctl
;
1125 if (!i2c_check_functionality(client
->adapter
, i2c_funcs
))
1128 pctl
= devm_kzalloc(dev
, sizeof(*pctl
), GFP_KERNEL
);
1132 i2c_set_clientdata(client
, pctl
);
1135 pctl
->client
= client
;
1137 pctl
->data
= i2c_get_match_data(client
);
1141 pctl
->regmap
= devm_regmap_init(dev
, NULL
, pctl
,
1142 &sx150x_regmap_config
);
1143 if (IS_ERR(pctl
->regmap
)) {
1144 ret
= PTR_ERR(pctl
->regmap
);
1145 dev_err(dev
, "Failed to allocate register map: %d\n",
1150 mutex_init(&pctl
->lock
);
1152 ret
= sx150x_init_hw(pctl
);
1157 pctl
->pinctrl_desc
.name
= "sx150x-pinctrl";
1158 pctl
->pinctrl_desc
.pctlops
= &sx150x_pinctrl_ops
;
1159 pctl
->pinctrl_desc
.confops
= &sx150x_pinconf_ops
;
1160 pctl
->pinctrl_desc
.pins
= pctl
->data
->pins
;
1161 pctl
->pinctrl_desc
.npins
= pctl
->data
->npins
;
1162 pctl
->pinctrl_desc
.owner
= THIS_MODULE
;
1164 ret
= devm_pinctrl_register_and_init(dev
, &pctl
->pinctrl_desc
,
1165 pctl
, &pctl
->pctldev
);
1167 dev_err(dev
, "Failed to register pinctrl device\n");
1171 /* Register GPIO controller */
1172 pctl
->gpio
.base
= -1;
1173 pctl
->gpio
.ngpio
= pctl
->data
->npins
;
1174 pctl
->gpio
.get_direction
= sx150x_gpio_get_direction
;
1175 pctl
->gpio
.direction_input
= sx150x_gpio_direction_input
;
1176 pctl
->gpio
.direction_output
= sx150x_gpio_direction_output
;
1177 pctl
->gpio
.get
= sx150x_gpio_get
;
1178 pctl
->gpio
.set
= sx150x_gpio_set
;
1179 pctl
->gpio
.set_config
= gpiochip_generic_config
;
1180 pctl
->gpio
.parent
= dev
;
1181 pctl
->gpio
.can_sleep
= true;
1182 pctl
->gpio
.label
= devm_kstrdup(dev
, client
->name
, GFP_KERNEL
);
1183 if (!pctl
->gpio
.label
)
1187 * Setting multiple pins is not safe when all pins are not
1188 * handled by the same regmap register. The oscio pin (present
1189 * on the SX150X_789 chips) lives in its own register, so
1190 * would require locking that is not in place at this time.
1192 if (pctl
->data
->model
!= SX150X_789
)
1193 pctl
->gpio
.set_multiple
= sx150x_gpio_set_multiple
;
1195 /* Add Interrupt support if an irq is specified */
1196 if (client
->irq
> 0) {
1197 struct gpio_irq_chip
*girq
;
1199 pctl
->irq
.masked
= ~0;
1200 pctl
->irq
.sense
= 0;
1202 * Because sx150x_irq_threaded_fn invokes all of the
1203 * nested interrupt handlers via handle_nested_irq,
1204 * any "handler" assigned to struct gpio_irq_chip
1205 * below is going to be ignored, so the choice of the
1206 * function does not matter that much.
1208 * We set it to handle_bad_irq to avoid confusion,
1209 * plus it will be instantly noticeable if it is ever
1210 * called (should not happen)
1212 girq
= &pctl
->gpio
.irq
;
1213 gpio_irq_chip_set_chip(girq
, &sx150x_irq_chip
);
1214 /* This will let us handle the parent IRQ in the driver */
1215 girq
->parent_handler
= NULL
;
1216 girq
->num_parents
= 0;
1217 girq
->parents
= NULL
;
1218 girq
->default_type
= IRQ_TYPE_NONE
;
1219 girq
->handler
= handle_bad_irq
;
1220 girq
->threaded
= true;
1222 ret
= devm_request_threaded_irq(dev
, client
->irq
, NULL
,
1223 sx150x_irq_thread_fn
,
1224 IRQF_ONESHOT
| IRQF_SHARED
|
1225 IRQF_TRIGGER_FALLING
,
1226 client
->name
, pctl
);
1231 ret
= devm_gpiochip_add_data(dev
, &pctl
->gpio
, pctl
);
1236 * Pin control functions need to be enabled AFTER registering the
1237 * GPIO chip because sx150x_pinconf_set() calls
1238 * sx150x_gpio_direction_output().
1240 ret
= pinctrl_enable(pctl
->pctldev
);
1242 dev_err(dev
, "Failed to enable pinctrl device\n");
1246 ret
= gpiochip_add_pin_range(&pctl
->gpio
, dev_name(dev
),
1247 0, 0, pctl
->data
->npins
);
1254 static struct i2c_driver sx150x_driver
= {
1256 .name
= "sx150x-pinctrl",
1257 .of_match_table
= sx150x_of_match
,
1259 .probe
= sx150x_probe
,
1260 .id_table
= sx150x_id
,
1263 static int __init
sx150x_init(void)
1265 return i2c_add_driver(&sx150x_driver
);
1267 subsys_initcall(sx150x_init
);