2 * Driver for Conexant Digicolor General Purpose Pin Mapping
4 * Author: Baruch Siach <baruch@tkos.co.il>
6 * Copyright (C) 2015 Paradox Innovation Ltd.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 * - GPIO interrupt support
15 * - Pin pad configuration (pull up/down, strength)
18 #include <linux/init.h>
19 #include <linux/platform_device.h>
21 #include <linux/of_device.h>
23 #include <linux/gpio.h>
24 #include <linux/gpio/driver.h>
25 #include <linux/spinlock.h>
26 #include <linux/pinctrl/machine.h>
27 #include <linux/pinctrl/pinconf.h>
28 #include <linux/pinctrl/pinconf-generic.h>
29 #include <linux/pinctrl/pinctrl.h>
30 #include <linux/pinctrl/pinmux.h>
31 #include "pinctrl-utils.h"
33 #define DRIVER_NAME "pinctrl-digicolor"
35 #define GP_CLIENTSEL(clct) ((clct)*8 + 0x20)
36 #define GP_DRIVE0(clct) (GP_CLIENTSEL(clct) + 2)
37 #define GP_OUTPUT0(clct) (GP_CLIENTSEL(clct) + 3)
38 #define GP_INPUT(clct) (GP_CLIENTSEL(clct) + 6)
40 #define PIN_COLLECTIONS ('R' - 'A' + 1)
41 #define PINS_PER_COLLECTION 8
42 #define PINS_COUNT (PIN_COLLECTIONS * PINS_PER_COLLECTION)
47 struct pinctrl_dev
*pctl
;
49 struct pinctrl_desc
*desc
;
50 const char *pin_names
[PINS_COUNT
];
52 struct gpio_chip chip
;
56 static int dc_get_groups_count(struct pinctrl_dev
*pctldev
)
61 static const char *dc_get_group_name(struct pinctrl_dev
*pctldev
,
64 struct dc_pinmap
*pmap
= pinctrl_dev_get_drvdata(pctldev
);
66 /* Exactly one group per pin */
67 return pmap
->desc
->pins
[selector
].name
;
70 static int dc_get_group_pins(struct pinctrl_dev
*pctldev
, unsigned selector
,
71 const unsigned **pins
,
74 struct dc_pinmap
*pmap
= pinctrl_dev_get_drvdata(pctldev
);
76 *pins
= &pmap
->desc
->pins
[selector
].number
;
82 static const struct pinctrl_ops dc_pinctrl_ops
= {
83 .get_groups_count
= dc_get_groups_count
,
84 .get_group_name
= dc_get_group_name
,
85 .get_group_pins
= dc_get_group_pins
,
86 .dt_node_to_map
= pinconf_generic_dt_node_to_map_pin
,
87 .dt_free_map
= pinctrl_utils_free_map
,
90 static const char *const dc_functions
[] = {
97 static int dc_get_functions_count(struct pinctrl_dev
*pctldev
)
99 return ARRAY_SIZE(dc_functions
);
102 static const char *dc_get_fname(struct pinctrl_dev
*pctldev
, unsigned selector
)
104 return dc_functions
[selector
];
107 static int dc_get_groups(struct pinctrl_dev
*pctldev
, unsigned selector
,
108 const char * const **groups
,
109 unsigned * const num_groups
)
111 struct dc_pinmap
*pmap
= pinctrl_dev_get_drvdata(pctldev
);
113 *groups
= pmap
->pin_names
;
114 *num_groups
= PINS_COUNT
;
119 static void dc_client_sel(int pin_num
, int *reg
, int *bit
)
121 *bit
= (pin_num
% PINS_PER_COLLECTION
) * 2;
122 *reg
= GP_CLIENTSEL(pin_num
/PINS_PER_COLLECTION
);
124 if (*bit
>= PINS_PER_COLLECTION
) {
125 *bit
-= PINS_PER_COLLECTION
;
130 static int dc_set_mux(struct pinctrl_dev
*pctldev
, unsigned selector
,
133 struct dc_pinmap
*pmap
= pinctrl_dev_get_drvdata(pctldev
);
134 int bit_off
, reg_off
;
137 dc_client_sel(group
, ®_off
, &bit_off
);
139 reg
= readb_relaxed(pmap
->regs
+ reg_off
);
140 reg
&= ~(3 << bit_off
);
141 reg
|= (selector
<< bit_off
);
142 writeb_relaxed(reg
, pmap
->regs
+ reg_off
);
147 static int dc_pmx_request_gpio(struct pinctrl_dev
*pcdev
,
148 struct pinctrl_gpio_range
*range
,
151 struct dc_pinmap
*pmap
= pinctrl_dev_get_drvdata(pcdev
);
152 int bit_off
, reg_off
;
155 dc_client_sel(offset
, ®_off
, &bit_off
);
157 reg
= readb_relaxed(pmap
->regs
+ reg_off
);
158 if ((reg
& (3 << bit_off
)) != 0)
164 static const struct pinmux_ops dc_pmxops
= {
165 .get_functions_count
= dc_get_functions_count
,
166 .get_function_name
= dc_get_fname
,
167 .get_function_groups
= dc_get_groups
,
168 .set_mux
= dc_set_mux
,
169 .gpio_request_enable
= dc_pmx_request_gpio
,
172 static int dc_gpio_direction_input(struct gpio_chip
*chip
, unsigned gpio
)
174 struct dc_pinmap
*pmap
= gpiochip_get_data(chip
);
175 int reg_off
= GP_DRIVE0(gpio
/PINS_PER_COLLECTION
);
176 int bit_off
= gpio
% PINS_PER_COLLECTION
;
180 spin_lock_irqsave(&pmap
->lock
, flags
);
181 drive
= readb_relaxed(pmap
->regs
+ reg_off
);
182 drive
&= ~BIT(bit_off
);
183 writeb_relaxed(drive
, pmap
->regs
+ reg_off
);
184 spin_unlock_irqrestore(&pmap
->lock
, flags
);
189 static void dc_gpio_set(struct gpio_chip
*chip
, unsigned gpio
, int value
);
191 static int dc_gpio_direction_output(struct gpio_chip
*chip
, unsigned gpio
,
194 struct dc_pinmap
*pmap
= gpiochip_get_data(chip
);
195 int reg_off
= GP_DRIVE0(gpio
/PINS_PER_COLLECTION
);
196 int bit_off
= gpio
% PINS_PER_COLLECTION
;
200 dc_gpio_set(chip
, gpio
, value
);
202 spin_lock_irqsave(&pmap
->lock
, flags
);
203 drive
= readb_relaxed(pmap
->regs
+ reg_off
);
204 drive
|= BIT(bit_off
);
205 writeb_relaxed(drive
, pmap
->regs
+ reg_off
);
206 spin_unlock_irqrestore(&pmap
->lock
, flags
);
211 static int dc_gpio_get(struct gpio_chip
*chip
, unsigned gpio
)
213 struct dc_pinmap
*pmap
= gpiochip_get_data(chip
);
214 int reg_off
= GP_INPUT(gpio
/PINS_PER_COLLECTION
);
215 int bit_off
= gpio
% PINS_PER_COLLECTION
;
218 input
= readb_relaxed(pmap
->regs
+ reg_off
);
220 return !!(input
& BIT(bit_off
));
223 static void dc_gpio_set(struct gpio_chip
*chip
, unsigned gpio
, int value
)
225 struct dc_pinmap
*pmap
= gpiochip_get_data(chip
);
226 int reg_off
= GP_OUTPUT0(gpio
/PINS_PER_COLLECTION
);
227 int bit_off
= gpio
% PINS_PER_COLLECTION
;
231 spin_lock_irqsave(&pmap
->lock
, flags
);
232 output
= readb_relaxed(pmap
->regs
+ reg_off
);
234 output
|= BIT(bit_off
);
236 output
&= ~BIT(bit_off
);
237 writeb_relaxed(output
, pmap
->regs
+ reg_off
);
238 spin_unlock_irqrestore(&pmap
->lock
, flags
);
241 static int dc_gpiochip_add(struct dc_pinmap
*pmap
, struct device_node
*np
)
243 struct gpio_chip
*chip
= &pmap
->chip
;
246 chip
->label
= DRIVER_NAME
;
247 chip
->parent
= pmap
->dev
;
248 chip
->request
= gpiochip_generic_request
;
249 chip
->free
= gpiochip_generic_free
;
250 chip
->direction_input
= dc_gpio_direction_input
;
251 chip
->direction_output
= dc_gpio_direction_output
;
252 chip
->get
= dc_gpio_get
;
253 chip
->set
= dc_gpio_set
;
255 chip
->ngpio
= PINS_COUNT
;
257 chip
->of_gpio_n_cells
= 2;
259 spin_lock_init(&pmap
->lock
);
261 ret
= gpiochip_add_data(chip
, pmap
);
265 ret
= gpiochip_add_pin_range(chip
, dev_name(pmap
->dev
), 0, 0,
268 gpiochip_remove(chip
);
275 static int dc_pinctrl_probe(struct platform_device
*pdev
)
277 struct dc_pinmap
*pmap
;
279 struct pinctrl_pin_desc
*pins
;
280 struct pinctrl_desc
*pctl_desc
;
282 int name_len
= strlen("GP_xx") + 1;
285 pmap
= devm_kzalloc(&pdev
->dev
, sizeof(*pmap
), GFP_KERNEL
);
289 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
290 pmap
->regs
= devm_ioremap_resource(&pdev
->dev
, r
);
291 if (IS_ERR(pmap
->regs
))
292 return PTR_ERR(pmap
->regs
);
294 pins
= devm_kzalloc(&pdev
->dev
, sizeof(*pins
)*PINS_COUNT
, GFP_KERNEL
);
297 pin_names
= devm_kzalloc(&pdev
->dev
, name_len
* PINS_COUNT
,
302 for (i
= 0; i
< PIN_COLLECTIONS
; i
++) {
303 for (j
= 0; j
< PINS_PER_COLLECTION
; j
++) {
304 int pin_id
= i
*PINS_PER_COLLECTION
+ j
;
305 char *name
= &pin_names
[pin_id
* name_len
];
307 snprintf(name
, name_len
, "GP_%c%c", 'A'+i
, '0'+j
);
309 pins
[pin_id
].number
= pin_id
;
310 pins
[pin_id
].name
= name
;
311 pmap
->pin_names
[pin_id
] = name
;
315 pctl_desc
= devm_kzalloc(&pdev
->dev
, sizeof(*pctl_desc
), GFP_KERNEL
);
319 pctl_desc
->name
= DRIVER_NAME
,
320 pctl_desc
->owner
= THIS_MODULE
,
321 pctl_desc
->pctlops
= &dc_pinctrl_ops
,
322 pctl_desc
->pmxops
= &dc_pmxops
,
323 pctl_desc
->npins
= PINS_COUNT
;
324 pctl_desc
->pins
= pins
;
325 pmap
->desc
= pctl_desc
;
327 pmap
->dev
= &pdev
->dev
;
329 pmap
->pctl
= devm_pinctrl_register(&pdev
->dev
, pctl_desc
, pmap
);
330 if (IS_ERR(pmap
->pctl
)) {
331 dev_err(&pdev
->dev
, "pinctrl driver registration failed\n");
332 return PTR_ERR(pmap
->pctl
);
335 return dc_gpiochip_add(pmap
, pdev
->dev
.of_node
);
338 static const struct of_device_id dc_pinctrl_ids
[] = {
339 { .compatible
= "cnxt,cx92755-pinctrl" },
343 static struct platform_driver dc_pinctrl_driver
= {
346 .of_match_table
= dc_pinctrl_ids
,
347 .suppress_bind_attrs
= true,
349 .probe
= dc_pinctrl_probe
,
351 builtin_platform_driver(dc_pinctrl_driver
);