2 * Copyright (c) 2016, BayLibre, SAS. All rights reserved.
3 * Author: Neil Armstrong <narmstrong@baylibre.com>
5 * Copyright (c) 2010, Code Aurora Forum. All rights reserved.
7 * Driver for Semtech SX150X I2C GPIO Expanders
8 * The handling of the 4-bit chips (SX1501/SX1504/SX1507) is untested.
10 * Author: Gregory Bean <gbean@codeaurora.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 and
14 * only version 2 as published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
22 #include <linux/regmap.h>
23 #include <linux/i2c.h>
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
26 #include <linux/irq.h>
27 #include <linux/mutex.h>
28 #include <linux/slab.h>
30 #include <linux/of_device.h>
31 #include <linux/gpio/driver.h>
32 #include <linux/pinctrl/pinconf.h>
33 #include <linux/pinctrl/pinctrl.h>
34 #include <linux/pinctrl/pinmux.h>
35 #include <linux/pinctrl/pinconf-generic.h>
39 #include "pinctrl-utils.h"
41 /* The chip models of sx150x */
48 SX150X_789_REG_MISC_AUTOCLEAR_OFF
= 1 << 0,
49 SX150X_MAX_REGISTER
= 0xad,
50 SX150X_IRQ_TYPE_EDGE_RISING
= 0x1,
51 SX150X_IRQ_TYPE_EDGE_FALLING
= 0x2,
52 SX150X_789_RESET_KEY1
= 0x12,
53 SX150X_789_RESET_KEY2
= 0x34,
56 struct sx150x_123_pri
{
66 struct sx150x_456_pri
{
76 struct sx150x_789_pri
{
85 struct sx150x_device_data
{
96 struct sx150x_123_pri x123
;
97 struct sx150x_456_pri x456
;
98 struct sx150x_789_pri x789
;
100 const struct pinctrl_pin_desc
*pins
;
104 struct sx150x_pinctrl
{
106 struct i2c_client
*client
;
107 struct pinctrl_dev
*pctldev
;
108 struct pinctrl_desc pinctrl_desc
;
109 struct gpio_chip gpio
;
110 struct irq_chip irq_chip
;
111 struct regmap
*regmap
;
117 const struct sx150x_device_data
*data
;
120 static const struct pinctrl_pin_desc sx150x_4_pins
[] = {
121 PINCTRL_PIN(0, "gpio0"),
122 PINCTRL_PIN(1, "gpio1"),
123 PINCTRL_PIN(2, "gpio2"),
124 PINCTRL_PIN(3, "gpio3"),
125 PINCTRL_PIN(4, "oscio"),
128 static const struct pinctrl_pin_desc sx150x_8_pins
[] = {
129 PINCTRL_PIN(0, "gpio0"),
130 PINCTRL_PIN(1, "gpio1"),
131 PINCTRL_PIN(2, "gpio2"),
132 PINCTRL_PIN(3, "gpio3"),
133 PINCTRL_PIN(4, "gpio4"),
134 PINCTRL_PIN(5, "gpio5"),
135 PINCTRL_PIN(6, "gpio6"),
136 PINCTRL_PIN(7, "gpio7"),
137 PINCTRL_PIN(8, "oscio"),
140 static const struct pinctrl_pin_desc sx150x_16_pins
[] = {
141 PINCTRL_PIN(0, "gpio0"),
142 PINCTRL_PIN(1, "gpio1"),
143 PINCTRL_PIN(2, "gpio2"),
144 PINCTRL_PIN(3, "gpio3"),
145 PINCTRL_PIN(4, "gpio4"),
146 PINCTRL_PIN(5, "gpio5"),
147 PINCTRL_PIN(6, "gpio6"),
148 PINCTRL_PIN(7, "gpio7"),
149 PINCTRL_PIN(8, "gpio8"),
150 PINCTRL_PIN(9, "gpio9"),
151 PINCTRL_PIN(10, "gpio10"),
152 PINCTRL_PIN(11, "gpio11"),
153 PINCTRL_PIN(12, "gpio12"),
154 PINCTRL_PIN(13, "gpio13"),
155 PINCTRL_PIN(14, "gpio14"),
156 PINCTRL_PIN(15, "gpio15"),
157 PINCTRL_PIN(16, "oscio"),
160 static const struct sx150x_device_data sx1501q_device_data
= {
166 .reg_irq_mask
= 0x05,
170 .reg_pld_mode
= 0x10,
171 .reg_pld_table0
= 0x11,
172 .reg_pld_table2
= 0x13,
173 .reg_advanced
= 0xad,
176 .pins
= sx150x_4_pins
,
177 .npins
= 4, /* oscio not available */
180 static const struct sx150x_device_data sx1502q_device_data
= {
186 .reg_irq_mask
= 0x05,
190 .reg_pld_mode
= 0x10,
191 .reg_pld_table0
= 0x11,
192 .reg_pld_table1
= 0x12,
193 .reg_pld_table2
= 0x13,
194 .reg_pld_table3
= 0x14,
195 .reg_pld_table4
= 0x15,
196 .reg_advanced
= 0xad,
199 .pins
= sx150x_8_pins
,
200 .npins
= 8, /* oscio not available */
203 static const struct sx150x_device_data sx1503q_device_data
= {
209 .reg_irq_mask
= 0x08,
213 .reg_pld_mode
= 0x20,
214 .reg_pld_table0
= 0x22,
215 .reg_pld_table1
= 0x24,
216 .reg_pld_table2
= 0x26,
217 .reg_pld_table3
= 0x28,
218 .reg_pld_table4
= 0x2a,
219 .reg_advanced
= 0xad,
222 .pins
= sx150x_16_pins
,
223 .npins
= 16, /* oscio not available */
226 static const struct sx150x_device_data sx1504q_device_data
= {
232 .reg_irq_mask
= 0x05,
236 .reg_pld_mode
= 0x10,
237 .reg_pld_table0
= 0x11,
238 .reg_pld_table2
= 0x13,
241 .pins
= sx150x_4_pins
,
242 .npins
= 4, /* oscio not available */
245 static const struct sx150x_device_data sx1505q_device_data
= {
251 .reg_irq_mask
= 0x05,
255 .reg_pld_mode
= 0x10,
256 .reg_pld_table0
= 0x11,
257 .reg_pld_table1
= 0x12,
258 .reg_pld_table2
= 0x13,
259 .reg_pld_table3
= 0x14,
260 .reg_pld_table4
= 0x15,
263 .pins
= sx150x_8_pins
,
264 .npins
= 8, /* oscio not available */
267 static const struct sx150x_device_data sx1506q_device_data
= {
273 .reg_irq_mask
= 0x08,
277 .reg_pld_mode
= 0x20,
278 .reg_pld_table0
= 0x22,
279 .reg_pld_table1
= 0x24,
280 .reg_pld_table2
= 0x26,
281 .reg_pld_table3
= 0x28,
282 .reg_pld_table4
= 0x2a,
283 .reg_advanced
= 0xad,
286 .pins
= sx150x_16_pins
,
287 .npins
= 16, /* oscio not available */
290 static const struct sx150x_device_data sx1507q_device_data
= {
296 .reg_irq_mask
= 0x09,
301 .reg_polarity
= 0x06,
307 .pins
= sx150x_4_pins
,
308 .npins
= ARRAY_SIZE(sx150x_4_pins
),
311 static const struct sx150x_device_data sx1508q_device_data
= {
317 .reg_irq_mask
= 0x09,
322 .reg_polarity
= 0x06,
328 .pins
= sx150x_8_pins
,
329 .npins
= ARRAY_SIZE(sx150x_8_pins
),
332 static const struct sx150x_device_data sx1509q_device_data
= {
338 .reg_irq_mask
= 0x12,
343 .reg_polarity
= 0x0c,
349 .pins
= sx150x_16_pins
,
350 .npins
= ARRAY_SIZE(sx150x_16_pins
),
353 static int sx150x_pinctrl_get_groups_count(struct pinctrl_dev
*pctldev
)
358 static const char *sx150x_pinctrl_get_group_name(struct pinctrl_dev
*pctldev
,
364 static int sx150x_pinctrl_get_group_pins(struct pinctrl_dev
*pctldev
,
366 const unsigned int **pins
,
367 unsigned int *num_pins
)
372 static const struct pinctrl_ops sx150x_pinctrl_ops
= {
373 .get_groups_count
= sx150x_pinctrl_get_groups_count
,
374 .get_group_name
= sx150x_pinctrl_get_group_name
,
375 .get_group_pins
= sx150x_pinctrl_get_group_pins
,
377 .dt_node_to_map
= pinconf_generic_dt_node_to_map_pin
,
378 .dt_free_map
= pinctrl_utils_free_map
,
382 static bool sx150x_pin_is_oscio(struct sx150x_pinctrl
*pctl
, unsigned int pin
)
384 if (pin
>= pctl
->data
->npins
)
387 /* OSCIO pin is only present in 789 devices */
388 if (pctl
->data
->model
!= SX150X_789
)
391 return !strcmp(pctl
->data
->pins
[pin
].name
, "oscio");
394 static int sx150x_gpio_get_direction(struct gpio_chip
*chip
,
397 struct sx150x_pinctrl
*pctl
= gpiochip_get_data(chip
);
401 if (sx150x_pin_is_oscio(pctl
, offset
))
404 ret
= regmap_read(pctl
->regmap
, pctl
->data
->reg_dir
, &value
);
408 return !!(value
& BIT(offset
));
411 static int sx150x_gpio_get(struct gpio_chip
*chip
, unsigned int offset
)
413 struct sx150x_pinctrl
*pctl
= gpiochip_get_data(chip
);
417 if (sx150x_pin_is_oscio(pctl
, offset
))
420 ret
= regmap_read(pctl
->regmap
, pctl
->data
->reg_data
, &value
);
424 return !!(value
& BIT(offset
));
427 static int __sx150x_gpio_set(struct sx150x_pinctrl
*pctl
, unsigned int offset
,
430 return regmap_write_bits(pctl
->regmap
, pctl
->data
->reg_data
,
431 BIT(offset
), value
? BIT(offset
) : 0);
434 static int sx150x_gpio_oscio_set(struct sx150x_pinctrl
*pctl
,
437 return regmap_write(pctl
->regmap
,
438 pctl
->data
->pri
.x789
.reg_clock
,
439 (value
? 0x1f : 0x10));
442 static void sx150x_gpio_set(struct gpio_chip
*chip
, unsigned int offset
,
445 struct sx150x_pinctrl
*pctl
= gpiochip_get_data(chip
);
447 if (sx150x_pin_is_oscio(pctl
, offset
))
448 sx150x_gpio_oscio_set(pctl
, value
);
450 __sx150x_gpio_set(pctl
, offset
, value
);
454 static void sx150x_gpio_set_multiple(struct gpio_chip
*chip
,
458 struct sx150x_pinctrl
*pctl
= gpiochip_get_data(chip
);
460 regmap_write_bits(pctl
->regmap
, pctl
->data
->reg_data
, *mask
, *bits
);
463 static int sx150x_gpio_direction_input(struct gpio_chip
*chip
,
466 struct sx150x_pinctrl
*pctl
= gpiochip_get_data(chip
);
468 if (sx150x_pin_is_oscio(pctl
, offset
))
471 return regmap_write_bits(pctl
->regmap
,
473 BIT(offset
), BIT(offset
));
476 static int sx150x_gpio_direction_output(struct gpio_chip
*chip
,
477 unsigned int offset
, int value
)
479 struct sx150x_pinctrl
*pctl
= gpiochip_get_data(chip
);
482 if (sx150x_pin_is_oscio(pctl
, offset
))
483 return sx150x_gpio_oscio_set(pctl
, value
);
485 ret
= __sx150x_gpio_set(pctl
, offset
, value
);
489 return regmap_write_bits(pctl
->regmap
,
494 static void sx150x_irq_mask(struct irq_data
*d
)
496 struct sx150x_pinctrl
*pctl
=
497 gpiochip_get_data(irq_data_get_irq_chip_data(d
));
498 unsigned int n
= d
->hwirq
;
500 pctl
->irq
.masked
|= BIT(n
);
503 static void sx150x_irq_unmask(struct irq_data
*d
)
505 struct sx150x_pinctrl
*pctl
=
506 gpiochip_get_data(irq_data_get_irq_chip_data(d
));
507 unsigned int n
= d
->hwirq
;
509 pctl
->irq
.masked
&= ~BIT(n
);
512 static void sx150x_irq_set_sense(struct sx150x_pinctrl
*pctl
,
513 unsigned int line
, unsigned int sense
)
516 * Every interrupt line is represented by two bits shifted
517 * proportionally to the line number
519 const unsigned int n
= line
* 2;
520 const unsigned int mask
= ~((SX150X_IRQ_TYPE_EDGE_RISING
|
521 SX150X_IRQ_TYPE_EDGE_FALLING
) << n
);
523 pctl
->irq
.sense
&= mask
;
524 pctl
->irq
.sense
|= sense
<< n
;
527 static int sx150x_irq_set_type(struct irq_data
*d
, unsigned int flow_type
)
529 struct sx150x_pinctrl
*pctl
=
530 gpiochip_get_data(irq_data_get_irq_chip_data(d
));
531 unsigned int n
, val
= 0;
533 if (flow_type
& (IRQ_TYPE_LEVEL_HIGH
| IRQ_TYPE_LEVEL_LOW
))
538 if (flow_type
& IRQ_TYPE_EDGE_RISING
)
539 val
|= SX150X_IRQ_TYPE_EDGE_RISING
;
540 if (flow_type
& IRQ_TYPE_EDGE_FALLING
)
541 val
|= SX150X_IRQ_TYPE_EDGE_FALLING
;
543 sx150x_irq_set_sense(pctl
, n
, val
);
547 static irqreturn_t
sx150x_irq_thread_fn(int irq
, void *dev_id
)
549 struct sx150x_pinctrl
*pctl
= (struct sx150x_pinctrl
*)dev_id
;
550 unsigned long n
, status
;
554 err
= regmap_read(pctl
->regmap
, pctl
->data
->reg_irq_src
, &val
);
558 err
= regmap_write(pctl
->regmap
, pctl
->data
->reg_irq_src
, val
);
563 for_each_set_bit(n
, &status
, pctl
->data
->ngpios
)
564 handle_nested_irq(irq_find_mapping(pctl
->gpio
.irq
.domain
, n
));
569 static void sx150x_irq_bus_lock(struct irq_data
*d
)
571 struct sx150x_pinctrl
*pctl
=
572 gpiochip_get_data(irq_data_get_irq_chip_data(d
));
574 mutex_lock(&pctl
->lock
);
577 static void sx150x_irq_bus_sync_unlock(struct irq_data
*d
)
579 struct sx150x_pinctrl
*pctl
=
580 gpiochip_get_data(irq_data_get_irq_chip_data(d
));
582 regmap_write(pctl
->regmap
, pctl
->data
->reg_irq_mask
, pctl
->irq
.masked
);
583 regmap_write(pctl
->regmap
, pctl
->data
->reg_sense
, pctl
->irq
.sense
);
584 mutex_unlock(&pctl
->lock
);
587 static int sx150x_pinconf_get(struct pinctrl_dev
*pctldev
, unsigned int pin
,
588 unsigned long *config
)
590 struct sx150x_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
591 unsigned int param
= pinconf_to_config_param(*config
);
596 if (sx150x_pin_is_oscio(pctl
, pin
)) {
598 case PIN_CONFIG_DRIVE_PUSH_PULL
:
599 case PIN_CONFIG_OUTPUT
:
600 ret
= regmap_read(pctl
->regmap
,
601 pctl
->data
->pri
.x789
.reg_clock
,
606 if (param
== PIN_CONFIG_DRIVE_PUSH_PULL
)
607 arg
= (data
& 0x1f) ? 1 : 0;
609 if ((data
& 0x1f) == 0x1f)
611 else if ((data
& 0x1f) == 0x10)
626 case PIN_CONFIG_BIAS_PULL_DOWN
:
627 ret
= regmap_read(pctl
->regmap
,
628 pctl
->data
->reg_pulldn
,
641 case PIN_CONFIG_BIAS_PULL_UP
:
642 ret
= regmap_read(pctl
->regmap
,
643 pctl
->data
->reg_pullup
,
656 case PIN_CONFIG_DRIVE_OPEN_DRAIN
:
657 if (pctl
->data
->model
!= SX150X_789
)
660 ret
= regmap_read(pctl
->regmap
,
661 pctl
->data
->pri
.x789
.reg_drain
,
674 case PIN_CONFIG_DRIVE_PUSH_PULL
:
675 if (pctl
->data
->model
!= SX150X_789
)
678 ret
= regmap_read(pctl
->regmap
,
679 pctl
->data
->pri
.x789
.reg_drain
,
693 case PIN_CONFIG_OUTPUT
:
694 ret
= sx150x_gpio_get_direction(&pctl
->gpio
, pin
);
701 ret
= sx150x_gpio_get(&pctl
->gpio
, pin
);
713 *config
= pinconf_to_config_packed(param
, arg
);
718 static int sx150x_pinconf_set(struct pinctrl_dev
*pctldev
, unsigned int pin
,
719 unsigned long *configs
, unsigned int num_configs
)
721 struct sx150x_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
722 enum pin_config_param param
;
727 for (i
= 0; i
< num_configs
; i
++) {
728 param
= pinconf_to_config_param(configs
[i
]);
729 arg
= pinconf_to_config_argument(configs
[i
]);
731 if (sx150x_pin_is_oscio(pctl
, pin
)) {
732 if (param
== PIN_CONFIG_OUTPUT
) {
733 ret
= sx150x_gpio_direction_output(&pctl
->gpio
,
744 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT
:
745 case PIN_CONFIG_BIAS_DISABLE
:
746 ret
= regmap_write_bits(pctl
->regmap
,
747 pctl
->data
->reg_pulldn
,
752 ret
= regmap_write_bits(pctl
->regmap
,
753 pctl
->data
->reg_pullup
,
760 case PIN_CONFIG_BIAS_PULL_UP
:
761 ret
= regmap_write_bits(pctl
->regmap
,
762 pctl
->data
->reg_pullup
,
769 case PIN_CONFIG_BIAS_PULL_DOWN
:
770 ret
= regmap_write_bits(pctl
->regmap
,
771 pctl
->data
->reg_pulldn
,
778 case PIN_CONFIG_DRIVE_OPEN_DRAIN
:
779 if (pctl
->data
->model
!= SX150X_789
||
780 sx150x_pin_is_oscio(pctl
, pin
))
783 ret
= regmap_write_bits(pctl
->regmap
,
784 pctl
->data
->pri
.x789
.reg_drain
,
791 case PIN_CONFIG_DRIVE_PUSH_PULL
:
792 if (pctl
->data
->model
!= SX150X_789
||
793 sx150x_pin_is_oscio(pctl
, pin
))
796 ret
= regmap_write_bits(pctl
->regmap
,
797 pctl
->data
->pri
.x789
.reg_drain
,
804 case PIN_CONFIG_OUTPUT
:
805 ret
= sx150x_gpio_direction_output(&pctl
->gpio
,
815 } /* for each config */
820 static const struct pinconf_ops sx150x_pinconf_ops
= {
821 .pin_config_get
= sx150x_pinconf_get
,
822 .pin_config_set
= sx150x_pinconf_set
,
826 static const struct i2c_device_id sx150x_id
[] = {
827 {"sx1501q", (kernel_ulong_t
) &sx1501q_device_data
},
828 {"sx1502q", (kernel_ulong_t
) &sx1502q_device_data
},
829 {"sx1503q", (kernel_ulong_t
) &sx1503q_device_data
},
830 {"sx1504q", (kernel_ulong_t
) &sx1504q_device_data
},
831 {"sx1505q", (kernel_ulong_t
) &sx1505q_device_data
},
832 {"sx1506q", (kernel_ulong_t
) &sx1506q_device_data
},
833 {"sx1507q", (kernel_ulong_t
) &sx1507q_device_data
},
834 {"sx1508q", (kernel_ulong_t
) &sx1508q_device_data
},
835 {"sx1509q", (kernel_ulong_t
) &sx1509q_device_data
},
839 static const struct of_device_id sx150x_of_match
[] = {
840 { .compatible
= "semtech,sx1501q", .data
= &sx1501q_device_data
},
841 { .compatible
= "semtech,sx1502q", .data
= &sx1502q_device_data
},
842 { .compatible
= "semtech,sx1503q", .data
= &sx1503q_device_data
},
843 { .compatible
= "semtech,sx1504q", .data
= &sx1504q_device_data
},
844 { .compatible
= "semtech,sx1505q", .data
= &sx1505q_device_data
},
845 { .compatible
= "semtech,sx1506q", .data
= &sx1506q_device_data
},
846 { .compatible
= "semtech,sx1507q", .data
= &sx1507q_device_data
},
847 { .compatible
= "semtech,sx1508q", .data
= &sx1508q_device_data
},
848 { .compatible
= "semtech,sx1509q", .data
= &sx1509q_device_data
},
852 static int sx150x_reset(struct sx150x_pinctrl
*pctl
)
856 err
= i2c_smbus_write_byte_data(pctl
->client
,
857 pctl
->data
->pri
.x789
.reg_reset
,
858 SX150X_789_RESET_KEY1
);
862 err
= i2c_smbus_write_byte_data(pctl
->client
,
863 pctl
->data
->pri
.x789
.reg_reset
,
864 SX150X_789_RESET_KEY2
);
868 static int sx150x_init_misc(struct sx150x_pinctrl
*pctl
)
872 switch (pctl
->data
->model
) {
874 reg
= pctl
->data
->pri
.x789
.reg_misc
;
875 value
= SX150X_789_REG_MISC_AUTOCLEAR_OFF
;
878 reg
= pctl
->data
->pri
.x456
.reg_advanced
;
882 * Only SX1506 has RegAdvanced, SX1504/5 are expected
883 * to initialize this offset to zero
889 reg
= pctl
->data
->pri
.x123
.reg_advanced
;
893 WARN(1, "Unknown chip model %d\n", pctl
->data
->model
);
897 return regmap_write(pctl
->regmap
, reg
, value
);
900 static int sx150x_init_hw(struct sx150x_pinctrl
*pctl
)
903 [SX150X_789
] = pctl
->data
->pri
.x789
.reg_polarity
,
904 [SX150X_456
] = pctl
->data
->pri
.x456
.reg_pld_mode
,
905 [SX150X_123
] = pctl
->data
->pri
.x123
.reg_pld_mode
,
909 if (pctl
->data
->model
== SX150X_789
&&
910 of_property_read_bool(pctl
->dev
->of_node
, "semtech,probe-reset")) {
911 err
= sx150x_reset(pctl
);
916 err
= sx150x_init_misc(pctl
);
920 /* Set all pins to work in normal mode */
921 return regmap_write(pctl
->regmap
, reg
[pctl
->data
->model
], 0);
924 static int sx150x_regmap_reg_width(struct sx150x_pinctrl
*pctl
,
927 const struct sx150x_device_data
*data
= pctl
->data
;
929 if (reg
== data
->reg_sense
) {
931 * RegSense packs two bits of configuration per GPIO,
932 * so we'd need to read twice as many bits as there
933 * are GPIO in our chip
935 return 2 * data
->ngpios
;
936 } else if ((data
->model
== SX150X_789
&&
937 (reg
== data
->pri
.x789
.reg_misc
||
938 reg
== data
->pri
.x789
.reg_clock
||
939 reg
== data
->pri
.x789
.reg_reset
))
941 (data
->model
== SX150X_123
&&
942 reg
== data
->pri
.x123
.reg_advanced
)
944 (data
->model
== SX150X_456
&&
945 data
->pri
.x456
.reg_advanced
&&
946 reg
== data
->pri
.x456
.reg_advanced
)) {
953 static unsigned int sx150x_maybe_swizzle(struct sx150x_pinctrl
*pctl
,
954 unsigned int reg
, unsigned int val
)
957 const struct sx150x_device_data
*data
= pctl
->data
;
960 * Whereas SX1509 presents RegSense in a simple layout as such:
961 * reg [ f f e e d d c c ]
962 * reg + 1 [ b b a a 9 9 8 8 ]
963 * reg + 2 [ 7 7 6 6 5 5 4 4 ]
964 * reg + 3 [ 3 3 2 2 1 1 0 0 ]
966 * SX1503 and SX1506 deviate from that data layout, instead storing
967 * their contents as follows:
969 * reg [ f f e e d d c c ]
970 * reg + 1 [ 7 7 6 6 5 5 4 4 ]
971 * reg + 2 [ b b a a 9 9 8 8 ]
972 * reg + 3 [ 3 3 2 2 1 1 0 0 ]
974 * so, taking that into account, we swap two
975 * inner bytes of a 4-byte result
978 if (reg
== data
->reg_sense
&&
979 data
->ngpios
== 16 &&
980 (data
->model
== SX150X_123
||
981 data
->model
== SX150X_456
)) {
982 a
= val
& 0x00ff0000;
983 b
= val
& 0x0000ff00;
994 * In order to mask the differences between 16 and 8 bit expander
995 * devices we set up a sligthly ficticious regmap that pretends to be
996 * a set of 32-bit (to accomodate RegSenseLow/RegSenseHigh
997 * pair/quartet) registers and transparently reconstructs those
998 * registers via multiple I2C/SMBus reads
1000 * This way the rest of the driver code, interfacing with the chip via
1001 * regmap API, can work assuming that each GPIO pin is represented by
1002 * a group of bits at an offset proportional to GPIO number within a
1005 static int sx150x_regmap_reg_read(void *context
, unsigned int reg
,
1006 unsigned int *result
)
1009 struct sx150x_pinctrl
*pctl
= context
;
1010 struct i2c_client
*i2c
= pctl
->client
;
1011 const int width
= sx150x_regmap_reg_width(pctl
, reg
);
1012 unsigned int idx
, val
;
1015 * There are four potential cases covered by this function:
1017 * 1) 8-pin chip, single configuration bit register
1019 * This is trivial the code below just needs to read:
1020 * reg [ 7 6 5 4 3 2 1 0 ]
1022 * 2) 8-pin chip, double configuration bit register (RegSense)
1024 * The read will be done as follows:
1025 * reg [ 7 7 6 6 5 5 4 4 ]
1026 * reg + 1 [ 3 3 2 2 1 1 0 0 ]
1028 * 3) 16-pin chip, single configuration bit register
1030 * The read will be done as follows:
1031 * reg [ f e d c b a 9 8 ]
1032 * reg + 1 [ 7 6 5 4 3 2 1 0 ]
1034 * 4) 16-pin chip, double configuration bit register (RegSense)
1036 * The read will be done as follows:
1037 * reg [ f f e e d d c c ]
1038 * reg + 1 [ b b a a 9 9 8 8 ]
1039 * reg + 2 [ 7 7 6 6 5 5 4 4 ]
1040 * reg + 3 [ 3 3 2 2 1 1 0 0 ]
1043 for (n
= width
, val
= 0, idx
= reg
; n
> 0; n
-= 8, idx
++) {
1046 ret
= i2c_smbus_read_byte_data(i2c
, idx
);
1053 *result
= sx150x_maybe_swizzle(pctl
, reg
, val
);
1058 static int sx150x_regmap_reg_write(void *context
, unsigned int reg
,
1062 struct sx150x_pinctrl
*pctl
= context
;
1063 struct i2c_client
*i2c
= pctl
->client
;
1064 const int width
= sx150x_regmap_reg_width(pctl
, reg
);
1066 val
= sx150x_maybe_swizzle(pctl
, reg
, val
);
1068 n
= (width
- 1) & ~7;
1070 const u8 byte
= (val
>> n
) & 0xff;
1072 ret
= i2c_smbus_write_byte_data(i2c
, reg
, byte
);
1083 static bool sx150x_reg_volatile(struct device
*dev
, unsigned int reg
)
1085 struct sx150x_pinctrl
*pctl
= i2c_get_clientdata(to_i2c_client(dev
));
1087 return reg
== pctl
->data
->reg_irq_src
|| reg
== pctl
->data
->reg_data
;
1090 static const struct regmap_config sx150x_regmap_config
= {
1094 .cache_type
= REGCACHE_RBTREE
,
1096 .reg_read
= sx150x_regmap_reg_read
,
1097 .reg_write
= sx150x_regmap_reg_write
,
1099 .max_register
= SX150X_MAX_REGISTER
,
1100 .volatile_reg
= sx150x_reg_volatile
,
1103 static int sx150x_probe(struct i2c_client
*client
,
1104 const struct i2c_device_id
*id
)
1106 static const u32 i2c_funcs
= I2C_FUNC_SMBUS_BYTE_DATA
|
1107 I2C_FUNC_SMBUS_WRITE_WORD_DATA
;
1108 struct device
*dev
= &client
->dev
;
1109 struct sx150x_pinctrl
*pctl
;
1112 if (!i2c_check_functionality(client
->adapter
, i2c_funcs
))
1115 pctl
= devm_kzalloc(dev
, sizeof(*pctl
), GFP_KERNEL
);
1119 i2c_set_clientdata(client
, pctl
);
1122 pctl
->client
= client
;
1125 pctl
->data
= of_device_get_match_data(dev
);
1127 pctl
->data
= (struct sx150x_device_data
*)id
->driver_data
;
1132 pctl
->regmap
= devm_regmap_init(dev
, NULL
, pctl
,
1133 &sx150x_regmap_config
);
1134 if (IS_ERR(pctl
->regmap
)) {
1135 ret
= PTR_ERR(pctl
->regmap
);
1136 dev_err(dev
, "Failed to allocate register map: %d\n",
1141 mutex_init(&pctl
->lock
);
1143 ret
= sx150x_init_hw(pctl
);
1148 pctl
->pinctrl_desc
.name
= "sx150x-pinctrl";
1149 pctl
->pinctrl_desc
.pctlops
= &sx150x_pinctrl_ops
;
1150 pctl
->pinctrl_desc
.confops
= &sx150x_pinconf_ops
;
1151 pctl
->pinctrl_desc
.pins
= pctl
->data
->pins
;
1152 pctl
->pinctrl_desc
.npins
= pctl
->data
->npins
;
1153 pctl
->pinctrl_desc
.owner
= THIS_MODULE
;
1155 ret
= devm_pinctrl_register_and_init(dev
, &pctl
->pinctrl_desc
,
1156 pctl
, &pctl
->pctldev
);
1158 dev_err(dev
, "Failed to register pinctrl device\n");
1162 ret
= pinctrl_enable(pctl
->pctldev
);
1164 dev_err(dev
, "Failed to enable pinctrl device\n");
1168 /* Register GPIO controller */
1169 pctl
->gpio
.label
= devm_kstrdup(dev
, client
->name
, GFP_KERNEL
);
1170 pctl
->gpio
.base
= -1;
1171 pctl
->gpio
.ngpio
= pctl
->data
->npins
;
1172 pctl
->gpio
.get_direction
= sx150x_gpio_get_direction
;
1173 pctl
->gpio
.direction_input
= sx150x_gpio_direction_input
;
1174 pctl
->gpio
.direction_output
= sx150x_gpio_direction_output
;
1175 pctl
->gpio
.get
= sx150x_gpio_get
;
1176 pctl
->gpio
.set
= sx150x_gpio_set
;
1177 pctl
->gpio
.set_config
= gpiochip_generic_config
;
1178 pctl
->gpio
.parent
= dev
;
1179 #ifdef CONFIG_OF_GPIO
1180 pctl
->gpio
.of_node
= dev
->of_node
;
1182 pctl
->gpio
.can_sleep
= true;
1184 * Setting multiple pins is not safe when all pins are not
1185 * handled by the same regmap register. The oscio pin (present
1186 * on the SX150X_789 chips) lives in its own register, so
1187 * would require locking that is not in place at this time.
1189 if (pctl
->data
->model
!= SX150X_789
)
1190 pctl
->gpio
.set_multiple
= sx150x_gpio_set_multiple
;
1192 ret
= devm_gpiochip_add_data(dev
, &pctl
->gpio
, pctl
);
1196 ret
= gpiochip_add_pin_range(&pctl
->gpio
, dev_name(dev
),
1197 0, 0, pctl
->data
->npins
);
1201 /* Add Interrupt support if an irq is specified */
1202 if (client
->irq
> 0) {
1203 pctl
->irq_chip
.name
= devm_kstrdup(dev
, client
->name
,
1205 pctl
->irq_chip
.irq_mask
= sx150x_irq_mask
;
1206 pctl
->irq_chip
.irq_unmask
= sx150x_irq_unmask
;
1207 pctl
->irq_chip
.irq_set_type
= sx150x_irq_set_type
;
1208 pctl
->irq_chip
.irq_bus_lock
= sx150x_irq_bus_lock
;
1209 pctl
->irq_chip
.irq_bus_sync_unlock
= sx150x_irq_bus_sync_unlock
;
1211 pctl
->irq
.masked
= ~0;
1212 pctl
->irq
.sense
= 0;
1215 * Because sx150x_irq_threaded_fn invokes all of the
1216 * nested interrrupt handlers via handle_nested_irq,
1217 * any "handler" passed to gpiochip_irqchip_add()
1218 * below is going to be ignored, so the choice of the
1219 * function does not matter that much.
1221 * We set it to handle_bad_irq to avoid confusion,
1222 * plus it will be instantly noticeable if it is ever
1223 * called (should not happen)
1225 ret
= gpiochip_irqchip_add_nested(&pctl
->gpio
,
1227 handle_bad_irq
, IRQ_TYPE_NONE
);
1229 dev_err(dev
, "could not connect irqchip to gpiochip\n");
1233 ret
= devm_request_threaded_irq(dev
, client
->irq
, NULL
,
1234 sx150x_irq_thread_fn
,
1235 IRQF_ONESHOT
| IRQF_SHARED
|
1236 IRQF_TRIGGER_FALLING
,
1237 pctl
->irq_chip
.name
, pctl
);
1241 gpiochip_set_nested_irqchip(&pctl
->gpio
,
1249 static struct i2c_driver sx150x_driver
= {
1251 .name
= "sx150x-pinctrl",
1252 .of_match_table
= of_match_ptr(sx150x_of_match
),
1254 .probe
= sx150x_probe
,
1255 .id_table
= sx150x_id
,
1258 static int __init
sx150x_init(void)
1260 return i2c_add_driver(&sx150x_driver
);
1262 subsys_initcall(sx150x_init
);