1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for Conexant Digicolor General Purpose Pin Mapping
5 * Author: Baruch Siach <baruch@tkos.co.il>
7 * Copyright (C) 2015 Paradox Innovation Ltd.
10 * - GPIO interrupt support
11 * - Pin pad configuration (pull up/down, strength)
14 #include <linux/gpio/driver.h>
15 #include <linux/init.h>
17 #include <linux/mod_devicetable.h>
18 #include <linux/platform_device.h>
19 #include <linux/spinlock.h>
21 #include <linux/pinctrl/machine.h>
22 #include <linux/pinctrl/pinconf.h>
23 #include <linux/pinctrl/pinconf-generic.h>
24 #include <linux/pinctrl/pinctrl.h>
25 #include <linux/pinctrl/pinmux.h>
27 #include "pinctrl-utils.h"
29 #define DRIVER_NAME "pinctrl-digicolor"
31 #define GP_CLIENTSEL(clct) ((clct)*8 + 0x20)
32 #define GP_DRIVE0(clct) (GP_CLIENTSEL(clct) + 2)
33 #define GP_OUTPUT0(clct) (GP_CLIENTSEL(clct) + 3)
34 #define GP_INPUT(clct) (GP_CLIENTSEL(clct) + 6)
36 #define PIN_COLLECTIONS ('R' - 'A' + 1)
37 #define PINS_PER_COLLECTION 8
38 #define PINS_COUNT (PIN_COLLECTIONS * PINS_PER_COLLECTION)
43 struct pinctrl_dev
*pctl
;
45 struct pinctrl_desc
*desc
;
46 const char *pin_names
[PINS_COUNT
];
48 struct gpio_chip chip
;
52 static int dc_get_groups_count(struct pinctrl_dev
*pctldev
)
57 static const char *dc_get_group_name(struct pinctrl_dev
*pctldev
,
60 struct dc_pinmap
*pmap
= pinctrl_dev_get_drvdata(pctldev
);
62 /* Exactly one group per pin */
63 return pmap
->desc
->pins
[selector
].name
;
66 static int dc_get_group_pins(struct pinctrl_dev
*pctldev
, unsigned selector
,
67 const unsigned **pins
,
70 struct dc_pinmap
*pmap
= pinctrl_dev_get_drvdata(pctldev
);
72 *pins
= &pmap
->desc
->pins
[selector
].number
;
78 static const struct pinctrl_ops dc_pinctrl_ops
= {
79 .get_groups_count
= dc_get_groups_count
,
80 .get_group_name
= dc_get_group_name
,
81 .get_group_pins
= dc_get_group_pins
,
82 .dt_node_to_map
= pinconf_generic_dt_node_to_map_pin
,
83 .dt_free_map
= pinctrl_utils_free_map
,
86 static const char *const dc_functions
[] = {
93 static int dc_get_functions_count(struct pinctrl_dev
*pctldev
)
95 return ARRAY_SIZE(dc_functions
);
98 static const char *dc_get_fname(struct pinctrl_dev
*pctldev
, unsigned selector
)
100 return dc_functions
[selector
];
103 static int dc_get_groups(struct pinctrl_dev
*pctldev
, unsigned selector
,
104 const char * const **groups
,
105 unsigned * const num_groups
)
107 struct dc_pinmap
*pmap
= pinctrl_dev_get_drvdata(pctldev
);
109 *groups
= pmap
->pin_names
;
110 *num_groups
= PINS_COUNT
;
115 static void dc_client_sel(int pin_num
, int *reg
, int *bit
)
117 *bit
= (pin_num
% PINS_PER_COLLECTION
) * 2;
118 *reg
= GP_CLIENTSEL(pin_num
/PINS_PER_COLLECTION
);
120 if (*bit
>= PINS_PER_COLLECTION
) {
121 *bit
-= PINS_PER_COLLECTION
;
126 static int dc_set_mux(struct pinctrl_dev
*pctldev
, unsigned selector
,
129 struct dc_pinmap
*pmap
= pinctrl_dev_get_drvdata(pctldev
);
130 int bit_off
, reg_off
;
133 dc_client_sel(group
, ®_off
, &bit_off
);
135 reg
= readb_relaxed(pmap
->regs
+ reg_off
);
136 reg
&= ~(3 << bit_off
);
137 reg
|= (selector
<< bit_off
);
138 writeb_relaxed(reg
, pmap
->regs
+ reg_off
);
143 static int dc_pmx_request_gpio(struct pinctrl_dev
*pcdev
,
144 struct pinctrl_gpio_range
*range
,
147 struct dc_pinmap
*pmap
= pinctrl_dev_get_drvdata(pcdev
);
148 int bit_off
, reg_off
;
151 dc_client_sel(offset
, ®_off
, &bit_off
);
153 reg
= readb_relaxed(pmap
->regs
+ reg_off
);
154 if ((reg
& (3 << bit_off
)) != 0)
160 static const struct pinmux_ops dc_pmxops
= {
161 .get_functions_count
= dc_get_functions_count
,
162 .get_function_name
= dc_get_fname
,
163 .get_function_groups
= dc_get_groups
,
164 .set_mux
= dc_set_mux
,
165 .gpio_request_enable
= dc_pmx_request_gpio
,
168 static int dc_gpio_direction_input(struct gpio_chip
*chip
, unsigned gpio
)
170 struct dc_pinmap
*pmap
= gpiochip_get_data(chip
);
171 int reg_off
= GP_DRIVE0(gpio
/PINS_PER_COLLECTION
);
172 int bit_off
= gpio
% PINS_PER_COLLECTION
;
176 spin_lock_irqsave(&pmap
->lock
, flags
);
177 drive
= readb_relaxed(pmap
->regs
+ reg_off
);
178 drive
&= ~BIT(bit_off
);
179 writeb_relaxed(drive
, pmap
->regs
+ reg_off
);
180 spin_unlock_irqrestore(&pmap
->lock
, flags
);
185 static void dc_gpio_set(struct gpio_chip
*chip
, unsigned gpio
, int value
);
187 static int dc_gpio_direction_output(struct gpio_chip
*chip
, unsigned gpio
,
190 struct dc_pinmap
*pmap
= gpiochip_get_data(chip
);
191 int reg_off
= GP_DRIVE0(gpio
/PINS_PER_COLLECTION
);
192 int bit_off
= gpio
% PINS_PER_COLLECTION
;
196 dc_gpio_set(chip
, gpio
, value
);
198 spin_lock_irqsave(&pmap
->lock
, flags
);
199 drive
= readb_relaxed(pmap
->regs
+ reg_off
);
200 drive
|= BIT(bit_off
);
201 writeb_relaxed(drive
, pmap
->regs
+ reg_off
);
202 spin_unlock_irqrestore(&pmap
->lock
, flags
);
207 static int dc_gpio_get(struct gpio_chip
*chip
, unsigned gpio
)
209 struct dc_pinmap
*pmap
= gpiochip_get_data(chip
);
210 int reg_off
= GP_INPUT(gpio
/PINS_PER_COLLECTION
);
211 int bit_off
= gpio
% PINS_PER_COLLECTION
;
214 input
= readb_relaxed(pmap
->regs
+ reg_off
);
216 return !!(input
& BIT(bit_off
));
219 static void dc_gpio_set(struct gpio_chip
*chip
, unsigned gpio
, int value
)
221 struct dc_pinmap
*pmap
= gpiochip_get_data(chip
);
222 int reg_off
= GP_OUTPUT0(gpio
/PINS_PER_COLLECTION
);
223 int bit_off
= gpio
% PINS_PER_COLLECTION
;
227 spin_lock_irqsave(&pmap
->lock
, flags
);
228 output
= readb_relaxed(pmap
->regs
+ reg_off
);
230 output
|= BIT(bit_off
);
232 output
&= ~BIT(bit_off
);
233 writeb_relaxed(output
, pmap
->regs
+ reg_off
);
234 spin_unlock_irqrestore(&pmap
->lock
, flags
);
237 static int dc_gpiochip_add(struct dc_pinmap
*pmap
)
239 struct gpio_chip
*chip
= &pmap
->chip
;
242 chip
->label
= DRIVER_NAME
;
243 chip
->parent
= pmap
->dev
;
244 chip
->request
= gpiochip_generic_request
;
245 chip
->free
= gpiochip_generic_free
;
246 chip
->direction_input
= dc_gpio_direction_input
;
247 chip
->direction_output
= dc_gpio_direction_output
;
248 chip
->get
= dc_gpio_get
;
249 chip
->set
= dc_gpio_set
;
251 chip
->ngpio
= PINS_COUNT
;
253 spin_lock_init(&pmap
->lock
);
255 ret
= gpiochip_add_data(chip
, pmap
);
259 ret
= gpiochip_add_pin_range(chip
, dev_name(pmap
->dev
), 0, 0,
262 gpiochip_remove(chip
);
269 static int dc_pinctrl_probe(struct platform_device
*pdev
)
271 struct dc_pinmap
*pmap
;
272 struct pinctrl_pin_desc
*pins
;
273 struct pinctrl_desc
*pctl_desc
;
275 int name_len
= strlen("GP_xx") + 1;
278 pmap
= devm_kzalloc(&pdev
->dev
, sizeof(*pmap
), GFP_KERNEL
);
282 pmap
->regs
= devm_platform_ioremap_resource(pdev
, 0);
283 if (IS_ERR(pmap
->regs
))
284 return PTR_ERR(pmap
->regs
);
286 pins
= devm_kcalloc(&pdev
->dev
, PINS_COUNT
, sizeof(*pins
),
290 pin_names
= devm_kcalloc(&pdev
->dev
, PINS_COUNT
, name_len
,
295 for (i
= 0; i
< PIN_COLLECTIONS
; i
++) {
296 for (j
= 0; j
< PINS_PER_COLLECTION
; j
++) {
297 int pin_id
= i
*PINS_PER_COLLECTION
+ j
;
298 char *name
= &pin_names
[pin_id
* name_len
];
300 snprintf(name
, name_len
, "GP_%c%c", 'A'+i
, '0'+j
);
302 pins
[pin_id
].number
= pin_id
;
303 pins
[pin_id
].name
= name
;
304 pmap
->pin_names
[pin_id
] = name
;
308 pctl_desc
= devm_kzalloc(&pdev
->dev
, sizeof(*pctl_desc
), GFP_KERNEL
);
312 pctl_desc
->name
= DRIVER_NAME
,
313 pctl_desc
->owner
= THIS_MODULE
,
314 pctl_desc
->pctlops
= &dc_pinctrl_ops
,
315 pctl_desc
->pmxops
= &dc_pmxops
,
316 pctl_desc
->npins
= PINS_COUNT
;
317 pctl_desc
->pins
= pins
;
318 pmap
->desc
= pctl_desc
;
320 pmap
->dev
= &pdev
->dev
;
322 pmap
->pctl
= devm_pinctrl_register(&pdev
->dev
, pctl_desc
, pmap
);
323 if (IS_ERR(pmap
->pctl
)) {
324 dev_err(&pdev
->dev
, "pinctrl driver registration failed\n");
325 return PTR_ERR(pmap
->pctl
);
328 return dc_gpiochip_add(pmap
);
331 static const struct of_device_id dc_pinctrl_ids
[] = {
332 { .compatible
= "cnxt,cx92755-pinctrl" },
336 static struct platform_driver dc_pinctrl_driver
= {
339 .of_match_table
= dc_pinctrl_ids
,
340 .suppress_bind_attrs
= true,
342 .probe
= dc_pinctrl_probe
,
344 builtin_platform_driver(dc_pinctrl_driver
);