blob: 3e6ee7f5be34d3af3f1143187ffe5ece70e5e053 [file] [log] [blame]
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +03001/*
Fenglin Wu0e407372016-07-19 13:25:15 +08002 * Copyright (c) 2012-2014, 2016, The Linux Foundation. All rights reserved.
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +03003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <linux/gpio.h>
15#include <linux/module.h>
16#include <linux/of.h>
Stephen Boydab4256c2015-11-18 11:33:17 -080017#include <linux/of_irq.h>
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +030018#include <linux/pinctrl/pinconf-generic.h>
19#include <linux/pinctrl/pinconf.h>
20#include <linux/pinctrl/pinmux.h>
21#include <linux/platform_device.h>
22#include <linux/regmap.h>
23#include <linux/slab.h>
24#include <linux/types.h>
25
26#include <dt-bindings/pinctrl/qcom,pmic-mpp.h>
27
28#include "../core.h"
29#include "../pinctrl-utils.h"
30
31#define PMIC_MPP_ADDRESS_RANGE 0x100
32
33/*
34 * Pull Up Values - it indicates whether a pull-up should be
35 * applied for bidirectional mode only. The hardware ignores the
36 * configuration when operating in other modes.
37 */
38#define PMIC_MPP_PULL_UP_0P6KOHM 0
39#define PMIC_MPP_PULL_UP_10KOHM 1
40#define PMIC_MPP_PULL_UP_30KOHM 2
41#define PMIC_MPP_PULL_UP_OPEN 3
42
43/* type registers base address bases */
44#define PMIC_MPP_REG_TYPE 0x4
45#define PMIC_MPP_REG_SUBTYPE 0x5
46
47/* mpp peripheral type and subtype values */
48#define PMIC_MPP_TYPE 0x11
49#define PMIC_MPP_SUBTYPE_4CH_NO_ANA_OUT 0x3
50#define PMIC_MPP_SUBTYPE_ULT_4CH_NO_ANA_OUT 0x4
51#define PMIC_MPP_SUBTYPE_4CH_NO_SINK 0x5
52#define PMIC_MPP_SUBTYPE_ULT_4CH_NO_SINK 0x6
53#define PMIC_MPP_SUBTYPE_4CH_FULL_FUNC 0x7
54#define PMIC_MPP_SUBTYPE_8CH_FULL_FUNC 0xf
55
56#define PMIC_MPP_REG_RT_STS 0x10
57#define PMIC_MPP_REG_RT_STS_VAL_MASK 0x1
58
59/* control register base address bases */
60#define PMIC_MPP_REG_MODE_CTL 0x40
61#define PMIC_MPP_REG_DIG_VIN_CTL 0x41
62#define PMIC_MPP_REG_DIG_PULL_CTL 0x42
63#define PMIC_MPP_REG_DIG_IN_CTL 0x43
64#define PMIC_MPP_REG_EN_CTL 0x46
Bjorn Andersson099f3e42015-07-14 23:40:33 -070065#define PMIC_MPP_REG_AOUT_CTL 0x48
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +030066#define PMIC_MPP_REG_AIN_CTL 0x4a
Bjorn Andersson0e948042015-06-17 23:47:28 -070067#define PMIC_MPP_REG_SINK_CTL 0x4c
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +030068
69/* PMIC_MPP_REG_MODE_CTL */
70#define PMIC_MPP_REG_MODE_VALUE_MASK 0x1
71#define PMIC_MPP_REG_MODE_FUNCTION_SHIFT 1
72#define PMIC_MPP_REG_MODE_FUNCTION_MASK 0x7
73#define PMIC_MPP_REG_MODE_DIR_SHIFT 4
74#define PMIC_MPP_REG_MODE_DIR_MASK 0x7
75
76/* PMIC_MPP_REG_DIG_VIN_CTL */
77#define PMIC_MPP_REG_VIN_SHIFT 0
78#define PMIC_MPP_REG_VIN_MASK 0x7
79
80/* PMIC_MPP_REG_DIG_PULL_CTL */
81#define PMIC_MPP_REG_PULL_SHIFT 0
82#define PMIC_MPP_REG_PULL_MASK 0x7
83
84/* PMIC_MPP_REG_EN_CTL */
85#define PMIC_MPP_REG_MASTER_EN_SHIFT 7
86
87/* PMIC_MPP_REG_AIN_CTL */
88#define PMIC_MPP_REG_AIN_ROUTE_SHIFT 0
89#define PMIC_MPP_REG_AIN_ROUTE_MASK 0x7
90
Fenglin Wu0e407372016-07-19 13:25:15 +080091/* PMIC_MPP_REG_SINK_CTL */
92#define PMIC_MPP_REG_CURRENT_SINK_MASK 0x7
93#define MPP_CURRENT_SINK_MA_STEP_SIZE 5
94
Bjorn Anderssoneaaf5dd2015-06-17 23:47:27 -070095#define PMIC_MPP_MODE_DIGITAL_INPUT 0
96#define PMIC_MPP_MODE_DIGITAL_OUTPUT 1
97#define PMIC_MPP_MODE_DIGITAL_BIDIR 2
98#define PMIC_MPP_MODE_ANALOG_BIDIR 3
99#define PMIC_MPP_MODE_ANALOG_INPUT 4
100#define PMIC_MPP_MODE_ANALOG_OUTPUT 5
101#define PMIC_MPP_MODE_CURRENT_SINK 6
102
Bjorn Anderssoneb5c1442015-06-17 23:47:30 -0700103#define PMIC_MPP_SELECTOR_NORMAL 0
104#define PMIC_MPP_SELECTOR_PAIRED 1
105#define PMIC_MPP_SELECTOR_DTEST_FIRST 4
106
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300107#define PMIC_MPP_PHYSICAL_OFFSET 1
108
109/* Qualcomm specific pin configurations */
110#define PMIC_MPP_CONF_AMUX_ROUTE (PIN_CONFIG_END + 1)
Bjorn Anderssoneb5c1442015-06-17 23:47:30 -0700111#define PMIC_MPP_CONF_ANALOG_LEVEL (PIN_CONFIG_END + 2)
112#define PMIC_MPP_CONF_DTEST_SELECTOR (PIN_CONFIG_END + 3)
113#define PMIC_MPP_CONF_PAIRED (PIN_CONFIG_END + 4)
Fenglin Wu9651d642016-07-19 17:03:42 +0800114#define PMIC_MPP_CONF_DTEST_BUFFER (PIN_CONFIG_END + 5)
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300115
116/**
117 * struct pmic_mpp_pad - keep current MPP settings
118 * @base: Address base in SPMI device.
119 * @irq: IRQ number which this MPP generate.
120 * @is_enabled: Set to false when MPP should be put in high Z state.
121 * @out_value: Cached pin output value.
122 * @output_enabled: Set to true if MPP output logic is enabled.
123 * @input_enabled: Set to true if MPP input buffer logic is enabled.
Bjorn Anderssoneb5c1442015-06-17 23:47:30 -0700124 * @paired: Pin operates in paired mode
Stephen Boydae6d54f2016-01-08 12:25:53 -0800125 * @has_pullup: Pin has support to configure pullup
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300126 * @num_sources: Number of power-sources supported by this MPP.
127 * @power_source: Current power-source used.
128 * @amux_input: Set the source for analog input.
Bjorn Andersson099f3e42015-07-14 23:40:33 -0700129 * @aout_level: Analog output level
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300130 * @pullup: Pullup resistor value. Valid in Bidirectional mode only.
131 * @function: See pmic_mpp_functions[].
Bjorn Andersson0e948042015-06-17 23:47:28 -0700132 * @drive_strength: Amount of current in sink mode
Bjorn Anderssoneb5c1442015-06-17 23:47:30 -0700133 * @dtest: DTEST route selector
Fenglin Wu9651d642016-07-19 17:03:42 +0800134 * @dtest_buffer: the DTEST buffer selection for digital input mode
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300135 */
136struct pmic_mpp_pad {
137 u16 base;
138 int irq;
139 bool is_enabled;
140 bool out_value;
141 bool output_enabled;
142 bool input_enabled;
Bjorn Anderssoneb5c1442015-06-17 23:47:30 -0700143 bool paired;
Stephen Boydae6d54f2016-01-08 12:25:53 -0800144 bool has_pullup;
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300145 unsigned int num_sources;
146 unsigned int power_source;
147 unsigned int amux_input;
Bjorn Andersson099f3e42015-07-14 23:40:33 -0700148 unsigned int aout_level;
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300149 unsigned int pullup;
150 unsigned int function;
Bjorn Andersson0e948042015-06-17 23:47:28 -0700151 unsigned int drive_strength;
Bjorn Anderssoneb5c1442015-06-17 23:47:30 -0700152 unsigned int dtest;
Fenglin Wu9651d642016-07-19 17:03:42 +0800153 unsigned int dtest_buffer;
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300154};
155
156struct pmic_mpp_state {
157 struct device *dev;
158 struct regmap *map;
159 struct pinctrl_dev *ctrl;
160 struct gpio_chip chip;
161};
162
Bjorn Anderssonba5f94c2015-06-17 23:47:25 -0700163static const struct pinconf_generic_params pmic_mpp_bindings[] = {
164 {"qcom,amux-route", PMIC_MPP_CONF_AMUX_ROUTE, 0},
Bjorn Andersson099f3e42015-07-14 23:40:33 -0700165 {"qcom,analog-level", PMIC_MPP_CONF_ANALOG_LEVEL, 0},
Bjorn Anderssoneb5c1442015-06-17 23:47:30 -0700166 {"qcom,dtest", PMIC_MPP_CONF_DTEST_SELECTOR, 0},
167 {"qcom,paired", PMIC_MPP_CONF_PAIRED, 0},
Fenglin Wu9651d642016-07-19 17:03:42 +0800168 {"qcom,dtest-buffer", PMIC_MPP_CONF_DTEST_BUFFER, 0},
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300169};
170
Bjorn Anderssonba5f94c2015-06-17 23:47:25 -0700171#ifdef CONFIG_DEBUG_FS
172static const struct pin_config_item pmic_conf_items[] = {
173 PCONFDUMP(PMIC_MPP_CONF_AMUX_ROUTE, "analog mux", NULL, true),
Bjorn Andersson099f3e42015-07-14 23:40:33 -0700174 PCONFDUMP(PMIC_MPP_CONF_ANALOG_LEVEL, "analog level", NULL, true),
Bjorn Anderssoneb5c1442015-06-17 23:47:30 -0700175 PCONFDUMP(PMIC_MPP_CONF_DTEST_SELECTOR, "dtest", NULL, true),
176 PCONFDUMP(PMIC_MPP_CONF_PAIRED, "paired", NULL, false),
Fenglin Wu9651d642016-07-19 17:03:42 +0800177 PCONFDUMP(PMIC_MPP_CONF_DTEST_BUFFER, "dtest buffer", NULL, true),
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300178};
Bjorn Anderssonba5f94c2015-06-17 23:47:25 -0700179#endif
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300180
181static const char *const pmic_mpp_groups[] = {
182 "mpp1", "mpp2", "mpp3", "mpp4", "mpp5", "mpp6", "mpp7", "mpp8",
183};
184
Bjorn Anderssoneb5c1442015-06-17 23:47:30 -0700185#define PMIC_MPP_DIGITAL 0
186#define PMIC_MPP_ANALOG 1
187#define PMIC_MPP_SINK 2
188
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300189static const char *const pmic_mpp_functions[] = {
Bjorn Anderssoneb5c1442015-06-17 23:47:30 -0700190 "digital", "analog", "sink"
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300191};
192
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300193static int pmic_mpp_read(struct pmic_mpp_state *state,
194 struct pmic_mpp_pad *pad, unsigned int addr)
195{
196 unsigned int val;
197 int ret;
198
199 ret = regmap_read(state->map, pad->base + addr, &val);
200 if (ret < 0)
201 dev_err(state->dev, "read 0x%x failed\n", addr);
202 else
203 ret = val;
204
205 return ret;
206}
207
208static int pmic_mpp_write(struct pmic_mpp_state *state,
209 struct pmic_mpp_pad *pad, unsigned int addr,
210 unsigned int val)
211{
212 int ret;
213
214 ret = regmap_write(state->map, pad->base + addr, val);
215 if (ret < 0)
216 dev_err(state->dev, "write 0x%x failed\n", addr);
217
218 return ret;
219}
220
221static int pmic_mpp_get_groups_count(struct pinctrl_dev *pctldev)
222{
223 /* Every PIN is a group */
224 return pctldev->desc->npins;
225}
226
227static const char *pmic_mpp_get_group_name(struct pinctrl_dev *pctldev,
228 unsigned pin)
229{
230 return pctldev->desc->pins[pin].name;
231}
232
233static int pmic_mpp_get_group_pins(struct pinctrl_dev *pctldev,
234 unsigned pin,
235 const unsigned **pins, unsigned *num_pins)
236{
237 *pins = &pctldev->desc->pins[pin].number;
238 *num_pins = 1;
239 return 0;
240}
241
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300242static const struct pinctrl_ops pmic_mpp_pinctrl_ops = {
243 .get_groups_count = pmic_mpp_get_groups_count,
244 .get_group_name = pmic_mpp_get_group_name,
245 .get_group_pins = pmic_mpp_get_group_pins,
Bjorn Anderssonba5f94c2015-06-17 23:47:25 -0700246 .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
Irina Tirdead32f7fd2016-03-31 14:44:42 +0300247 .dt_free_map = pinctrl_utils_free_map,
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300248};
249
250static int pmic_mpp_get_functions_count(struct pinctrl_dev *pctldev)
251{
252 return ARRAY_SIZE(pmic_mpp_functions);
253}
254
255static const char *pmic_mpp_get_function_name(struct pinctrl_dev *pctldev,
256 unsigned function)
257{
258 return pmic_mpp_functions[function];
259}
260
261static int pmic_mpp_get_function_groups(struct pinctrl_dev *pctldev,
262 unsigned function,
263 const char *const **groups,
264 unsigned *const num_qgroups)
265{
266 *groups = pmic_mpp_groups;
267 *num_qgroups = pctldev->desc->npins;
268 return 0;
269}
270
Bjorn Andersson0e948042015-06-17 23:47:28 -0700271static int pmic_mpp_write_mode_ctl(struct pmic_mpp_state *state,
272 struct pmic_mpp_pad *pad)
273{
Bjorn Anderssoneb5c1442015-06-17 23:47:30 -0700274 unsigned int mode;
275 unsigned int sel;
Bjorn Andersson0e948042015-06-17 23:47:28 -0700276 unsigned int val;
Bjorn Anderssoneb5c1442015-06-17 23:47:30 -0700277 unsigned int en;
Bjorn Andersson0e948042015-06-17 23:47:28 -0700278
Bjorn Anderssoneb5c1442015-06-17 23:47:30 -0700279 switch (pad->function) {
280 case PMIC_MPP_ANALOG:
281 if (pad->input_enabled && pad->output_enabled)
282 mode = PMIC_MPP_MODE_ANALOG_BIDIR;
283 else if (pad->input_enabled)
284 mode = PMIC_MPP_MODE_ANALOG_INPUT;
285 else
286 mode = PMIC_MPP_MODE_ANALOG_OUTPUT;
287 break;
288 case PMIC_MPP_DIGITAL:
289 if (pad->input_enabled && pad->output_enabled)
290 mode = PMIC_MPP_MODE_DIGITAL_BIDIR;
291 else if (pad->input_enabled)
292 mode = PMIC_MPP_MODE_DIGITAL_INPUT;
293 else
294 mode = PMIC_MPP_MODE_DIGITAL_OUTPUT;
295 break;
296 case PMIC_MPP_SINK:
297 default:
298 mode = PMIC_MPP_MODE_CURRENT_SINK;
299 break;
Bjorn Andersson0e948042015-06-17 23:47:28 -0700300 }
301
Bjorn Anderssoneb5c1442015-06-17 23:47:30 -0700302 if (pad->dtest)
303 sel = PMIC_MPP_SELECTOR_DTEST_FIRST + pad->dtest - 1;
304 else if (pad->paired)
305 sel = PMIC_MPP_SELECTOR_PAIRED;
306 else
307 sel = PMIC_MPP_SELECTOR_NORMAL;
308
309 en = !!pad->out_value;
310
311 val = mode << PMIC_MPP_REG_MODE_DIR_SHIFT |
312 sel << PMIC_MPP_REG_MODE_FUNCTION_SHIFT |
313 en;
Bjorn Andersson0e948042015-06-17 23:47:28 -0700314
315 return pmic_mpp_write(state, pad, PMIC_MPP_REG_MODE_CTL, val);
316}
317
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300318static int pmic_mpp_set_mux(struct pinctrl_dev *pctldev, unsigned function,
319 unsigned pin)
320{
321 struct pmic_mpp_state *state = pinctrl_dev_get_drvdata(pctldev);
322 struct pmic_mpp_pad *pad;
323 unsigned int val;
324 int ret;
325
326 pad = pctldev->desc->pins[pin].drv_data;
327
328 pad->function = function;
329
Bjorn Andersson0e948042015-06-17 23:47:28 -0700330 ret = pmic_mpp_write_mode_ctl(state, pad);
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300331
332 val = pad->is_enabled << PMIC_MPP_REG_MASTER_EN_SHIFT;
333
334 return pmic_mpp_write(state, pad, PMIC_MPP_REG_EN_CTL, val);
335}
336
337static const struct pinmux_ops pmic_mpp_pinmux_ops = {
338 .get_functions_count = pmic_mpp_get_functions_count,
339 .get_function_name = pmic_mpp_get_function_name,
340 .get_function_groups = pmic_mpp_get_function_groups,
341 .set_mux = pmic_mpp_set_mux,
342};
343
344static int pmic_mpp_config_get(struct pinctrl_dev *pctldev,
345 unsigned int pin, unsigned long *config)
346{
347 unsigned param = pinconf_to_config_param(*config);
348 struct pmic_mpp_pad *pad;
349 unsigned arg = 0;
350
351 pad = pctldev->desc->pins[pin].drv_data;
352
353 switch (param) {
354 case PIN_CONFIG_BIAS_DISABLE:
355 arg = pad->pullup == PMIC_MPP_PULL_UP_OPEN;
356 break;
357 case PIN_CONFIG_BIAS_PULL_UP:
358 switch (pad->pullup) {
359 case PMIC_MPP_PULL_UP_OPEN:
360 arg = 0;
361 break;
362 case PMIC_MPP_PULL_UP_0P6KOHM:
363 arg = 600;
364 break;
365 case PMIC_MPP_PULL_UP_10KOHM:
366 arg = 10000;
367 break;
368 case PMIC_MPP_PULL_UP_30KOHM:
369 arg = 30000;
370 break;
371 default:
372 return -EINVAL;
373 }
374 break;
375 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
376 arg = !pad->is_enabled;
377 break;
378 case PIN_CONFIG_POWER_SOURCE:
379 arg = pad->power_source;
380 break;
381 case PIN_CONFIG_INPUT_ENABLE:
382 arg = pad->input_enabled;
383 break;
384 case PIN_CONFIG_OUTPUT:
385 arg = pad->out_value;
386 break;
Bjorn Anderssoneb5c1442015-06-17 23:47:30 -0700387 case PMIC_MPP_CONF_DTEST_SELECTOR:
388 arg = pad->dtest;
389 break;
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300390 case PMIC_MPP_CONF_AMUX_ROUTE:
391 arg = pad->amux_input;
392 break;
Bjorn Anderssoneb5c1442015-06-17 23:47:30 -0700393 case PMIC_MPP_CONF_PAIRED:
394 arg = pad->paired;
395 break;
Bjorn Andersson0e948042015-06-17 23:47:28 -0700396 case PIN_CONFIG_DRIVE_STRENGTH:
397 arg = pad->drive_strength;
398 break;
Bjorn Andersson099f3e42015-07-14 23:40:33 -0700399 case PMIC_MPP_CONF_ANALOG_LEVEL:
400 arg = pad->aout_level;
401 break;
Fenglin Wu9651d642016-07-19 17:03:42 +0800402 case PMIC_MPP_CONF_DTEST_BUFFER:
403 arg = pad->dtest_buffer;
404 break;
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300405 default:
406 return -EINVAL;
407 }
408
409 /* Convert register value to pinconf value */
410 *config = pinconf_to_config_packed(param, arg);
411 return 0;
412}
413
414static int pmic_mpp_config_set(struct pinctrl_dev *pctldev, unsigned int pin,
415 unsigned long *configs, unsigned nconfs)
416{
417 struct pmic_mpp_state *state = pinctrl_dev_get_drvdata(pctldev);
418 struct pmic_mpp_pad *pad;
419 unsigned param, arg;
420 unsigned int val;
421 int i, ret;
422
423 pad = pctldev->desc->pins[pin].drv_data;
424
Bjorn Andersson7682b372015-06-17 23:47:26 -0700425 /* Make it possible to enable the pin, by not setting high impedance */
426 pad->is_enabled = true;
427
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300428 for (i = 0; i < nconfs; i++) {
429 param = pinconf_to_config_param(configs[i]);
430 arg = pinconf_to_config_argument(configs[i]);
431
432 switch (param) {
433 case PIN_CONFIG_BIAS_DISABLE:
434 pad->pullup = PMIC_MPP_PULL_UP_OPEN;
435 break;
436 case PIN_CONFIG_BIAS_PULL_UP:
437 switch (arg) {
438 case 600:
439 pad->pullup = PMIC_MPP_PULL_UP_0P6KOHM;
440 break;
441 case 10000:
442 pad->pullup = PMIC_MPP_PULL_UP_10KOHM;
443 break;
444 case 30000:
445 pad->pullup = PMIC_MPP_PULL_UP_30KOHM;
446 break;
447 default:
448 return -EINVAL;
449 }
450 break;
451 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
452 pad->is_enabled = false;
453 break;
454 case PIN_CONFIG_POWER_SOURCE:
455 if (arg >= pad->num_sources)
456 return -EINVAL;
457 pad->power_source = arg;
458 break;
459 case PIN_CONFIG_INPUT_ENABLE:
460 pad->input_enabled = arg ? true : false;
461 break;
462 case PIN_CONFIG_OUTPUT:
463 pad->output_enabled = true;
464 pad->out_value = arg;
465 break;
Bjorn Anderssoneb5c1442015-06-17 23:47:30 -0700466 case PMIC_MPP_CONF_DTEST_SELECTOR:
467 pad->dtest = arg;
468 break;
Bjorn Andersson0e948042015-06-17 23:47:28 -0700469 case PIN_CONFIG_DRIVE_STRENGTH:
Fenglin Wu0e407372016-07-19 13:25:15 +0800470 pad->drive_strength = arg;
Bjorn Andersson0e948042015-06-17 23:47:28 -0700471 break;
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300472 case PMIC_MPP_CONF_AMUX_ROUTE:
473 if (arg >= PMIC_MPP_AMUX_ROUTE_ABUS4)
474 return -EINVAL;
475 pad->amux_input = arg;
476 break;
Bjorn Andersson099f3e42015-07-14 23:40:33 -0700477 case PMIC_MPP_CONF_ANALOG_LEVEL:
478 pad->aout_level = arg;
479 break;
Bjorn Anderssoneb5c1442015-06-17 23:47:30 -0700480 case PMIC_MPP_CONF_PAIRED:
481 pad->paired = !!arg;
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300482 break;
Fenglin Wu9651d642016-07-19 17:03:42 +0800483 case PMIC_MPP_CONF_DTEST_BUFFER:
484 /*
485 * 0xf is the max value which selects
486 * 4 dtest rails simultaneously
487 */
488 if (arg > 0xf)
489 return -EINVAL;
490 pad->dtest_buffer = arg;
491 break;
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300492 default:
493 return -EINVAL;
494 }
495 }
496
497 val = pad->power_source << PMIC_MPP_REG_VIN_SHIFT;
498
499 ret = pmic_mpp_write(state, pad, PMIC_MPP_REG_DIG_VIN_CTL, val);
500 if (ret < 0)
501 return ret;
502
Fenglin Wu9651d642016-07-19 17:03:42 +0800503 val = pad->dtest_buffer;
504 ret = pmic_mpp_write(state, pad, PMIC_MPP_REG_DIG_IN_CTL, val);
505 if (ret < 0)
506 return ret;
507
Stephen Boydae6d54f2016-01-08 12:25:53 -0800508 if (pad->has_pullup) {
509 val = pad->pullup << PMIC_MPP_REG_PULL_SHIFT;
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300510
Stephen Boydae6d54f2016-01-08 12:25:53 -0800511 ret = pmic_mpp_write(state, pad, PMIC_MPP_REG_DIG_PULL_CTL,
512 val);
513 if (ret < 0)
514 return ret;
515 }
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300516
517 val = pad->amux_input & PMIC_MPP_REG_AIN_ROUTE_MASK;
518
519 ret = pmic_mpp_write(state, pad, PMIC_MPP_REG_AIN_CTL, val);
520 if (ret < 0)
521 return ret;
522
Bjorn Andersson099f3e42015-07-14 23:40:33 -0700523 ret = pmic_mpp_write(state, pad, PMIC_MPP_REG_AOUT_CTL, pad->aout_level);
524 if (ret < 0)
525 return ret;
526
Fenglin Wu0e407372016-07-19 13:25:15 +0800527 val = 0;
528 if (pad->drive_strength >= MPP_CURRENT_SINK_MA_STEP_SIZE)
529 val = DIV_ROUND_UP(pad->drive_strength,
530 MPP_CURRENT_SINK_MA_STEP_SIZE) - 1;
531
532 val &= PMIC_MPP_REG_CURRENT_SINK_MASK;
533 ret = pmic_mpp_write(state, pad, PMIC_MPP_REG_SINK_CTL, val);
534 if (ret < 0)
535 return ret;
536
Bjorn Andersson0e948042015-06-17 23:47:28 -0700537 ret = pmic_mpp_write_mode_ctl(state, pad);
Bjorn Andersson7682b372015-06-17 23:47:26 -0700538 if (ret < 0)
539 return ret;
540
541 val = pad->is_enabled << PMIC_MPP_REG_MASTER_EN_SHIFT;
542
543 return pmic_mpp_write(state, pad, PMIC_MPP_REG_EN_CTL, val);
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300544}
545
546static void pmic_mpp_config_dbg_show(struct pinctrl_dev *pctldev,
547 struct seq_file *s, unsigned pin)
548{
549 struct pmic_mpp_state *state = pinctrl_dev_get_drvdata(pctldev);
550 struct pmic_mpp_pad *pad;
Bjorn Andersson7682b372015-06-17 23:47:26 -0700551 int ret;
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300552
553 static const char *const biases[] = {
554 "0.6kOhm", "10kOhm", "30kOhm", "Disabled"
555 };
556
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300557 pad = pctldev->desc->pins[pin].drv_data;
558
559 seq_printf(s, " mpp%-2d:", pin + PMIC_MPP_PHYSICAL_OFFSET);
560
Bjorn Andersson7682b372015-06-17 23:47:26 -0700561 if (!pad->is_enabled) {
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300562 seq_puts(s, " ---");
563 } else {
564
565 if (pad->input_enabled) {
566 ret = pmic_mpp_read(state, pad, PMIC_MPP_REG_RT_STS);
Ivan T. Ivanov4e637ac2015-04-09 18:18:37 +0300567 if (ret < 0)
568 return;
569
570 ret &= PMIC_MPP_REG_RT_STS_VAL_MASK;
571 pad->out_value = ret;
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300572 }
573
574 seq_printf(s, " %-4s", pad->output_enabled ? "out" : "in");
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300575 seq_printf(s, " %-7s", pmic_mpp_functions[pad->function]);
576 seq_printf(s, " vin-%d", pad->power_source);
Bjorn Andersson099f3e42015-07-14 23:40:33 -0700577 seq_printf(s, " %d", pad->aout_level);
Stephen Boydae6d54f2016-01-08 12:25:53 -0800578 if (pad->has_pullup)
579 seq_printf(s, " %-8s", biases[pad->pullup]);
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300580 seq_printf(s, " %-4s", pad->out_value ? "high" : "low");
Bjorn Anderssoneb5c1442015-06-17 23:47:30 -0700581 if (pad->dtest)
582 seq_printf(s, " dtest%d", pad->dtest);
583 if (pad->paired)
584 seq_puts(s, " paired");
Fenglin Wu9651d642016-07-19 17:03:42 +0800585 if (pad->dtest_buffer)
586 seq_printf(s, " dtest buffer %d", pad->dtest_buffer);
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300587 }
588}
589
590static const struct pinconf_ops pmic_mpp_pinconf_ops = {
Bjorn Anderssonba5f94c2015-06-17 23:47:25 -0700591 .is_generic = true,
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300592 .pin_config_group_get = pmic_mpp_config_get,
593 .pin_config_group_set = pmic_mpp_config_set,
594 .pin_config_group_dbg_show = pmic_mpp_config_dbg_show,
595};
596
597static int pmic_mpp_direction_input(struct gpio_chip *chip, unsigned pin)
598{
Linus Walleij064761d2015-12-08 09:53:39 +0100599 struct pmic_mpp_state *state = gpiochip_get_data(chip);
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300600 unsigned long config;
601
602 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_ENABLE, 1);
603
604 return pmic_mpp_config_set(state->ctrl, pin, &config, 1);
605}
606
607static int pmic_mpp_direction_output(struct gpio_chip *chip,
608 unsigned pin, int val)
609{
Linus Walleij064761d2015-12-08 09:53:39 +0100610 struct pmic_mpp_state *state = gpiochip_get_data(chip);
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300611 unsigned long config;
612
613 config = pinconf_to_config_packed(PIN_CONFIG_OUTPUT, val);
614
615 return pmic_mpp_config_set(state->ctrl, pin, &config, 1);
616}
617
618static int pmic_mpp_get(struct gpio_chip *chip, unsigned pin)
619{
Linus Walleij064761d2015-12-08 09:53:39 +0100620 struct pmic_mpp_state *state = gpiochip_get_data(chip);
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300621 struct pmic_mpp_pad *pad;
622 int ret;
623
624 pad = state->ctrl->desc->pins[pin].drv_data;
625
626 if (pad->input_enabled) {
627 ret = pmic_mpp_read(state, pad, PMIC_MPP_REG_RT_STS);
628 if (ret < 0)
629 return ret;
630
631 pad->out_value = ret & PMIC_MPP_REG_RT_STS_VAL_MASK;
632 }
633
Linus Walleij59663a42015-12-21 16:31:06 +0100634 return !!pad->out_value;
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300635}
636
637static void pmic_mpp_set(struct gpio_chip *chip, unsigned pin, int value)
638{
Linus Walleij064761d2015-12-08 09:53:39 +0100639 struct pmic_mpp_state *state = gpiochip_get_data(chip);
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300640 unsigned long config;
641
642 config = pinconf_to_config_packed(PIN_CONFIG_OUTPUT, value);
643
644 pmic_mpp_config_set(state->ctrl, pin, &config, 1);
645}
646
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300647static int pmic_mpp_of_xlate(struct gpio_chip *chip,
648 const struct of_phandle_args *gpio_desc,
649 u32 *flags)
650{
651 if (chip->of_gpio_n_cells < 2)
652 return -EINVAL;
653
654 if (flags)
655 *flags = gpio_desc->args[1];
656
657 return gpio_desc->args[0] - PMIC_MPP_PHYSICAL_OFFSET;
658}
659
660static int pmic_mpp_to_irq(struct gpio_chip *chip, unsigned pin)
661{
Linus Walleij064761d2015-12-08 09:53:39 +0100662 struct pmic_mpp_state *state = gpiochip_get_data(chip);
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300663 struct pmic_mpp_pad *pad;
664
665 pad = state->ctrl->desc->pins[pin].drv_data;
666
667 return pad->irq;
668}
669
670static void pmic_mpp_dbg_show(struct seq_file *s, struct gpio_chip *chip)
671{
Linus Walleij064761d2015-12-08 09:53:39 +0100672 struct pmic_mpp_state *state = gpiochip_get_data(chip);
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300673 unsigned i;
674
675 for (i = 0; i < chip->ngpio; i++) {
676 pmic_mpp_config_dbg_show(state->ctrl, s, i);
677 seq_puts(s, "\n");
678 }
679}
680
681static const struct gpio_chip pmic_mpp_gpio_template = {
682 .direction_input = pmic_mpp_direction_input,
683 .direction_output = pmic_mpp_direction_output,
684 .get = pmic_mpp_get,
685 .set = pmic_mpp_set,
Jonas Gorski98c85d52015-10-11 17:34:19 +0200686 .request = gpiochip_generic_request,
687 .free = gpiochip_generic_free,
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300688 .of_xlate = pmic_mpp_of_xlate,
689 .to_irq = pmic_mpp_to_irq,
690 .dbg_show = pmic_mpp_dbg_show,
691};
692
693static int pmic_mpp_populate(struct pmic_mpp_state *state,
694 struct pmic_mpp_pad *pad)
695{
696 int type, subtype, val, dir;
Bjorn Anderssoneb5c1442015-06-17 23:47:30 -0700697 unsigned int sel;
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300698
699 type = pmic_mpp_read(state, pad, PMIC_MPP_REG_TYPE);
700 if (type < 0)
701 return type;
702
703 if (type != PMIC_MPP_TYPE) {
704 dev_err(state->dev, "incorrect block type 0x%x at 0x%x\n",
705 type, pad->base);
706 return -ENODEV;
707 }
708
709 subtype = pmic_mpp_read(state, pad, PMIC_MPP_REG_SUBTYPE);
710 if (subtype < 0)
711 return subtype;
712
713 switch (subtype) {
714 case PMIC_MPP_SUBTYPE_4CH_NO_ANA_OUT:
715 case PMIC_MPP_SUBTYPE_ULT_4CH_NO_ANA_OUT:
716 case PMIC_MPP_SUBTYPE_4CH_NO_SINK:
717 case PMIC_MPP_SUBTYPE_ULT_4CH_NO_SINK:
718 case PMIC_MPP_SUBTYPE_4CH_FULL_FUNC:
719 pad->num_sources = 4;
720 break;
721 case PMIC_MPP_SUBTYPE_8CH_FULL_FUNC:
722 pad->num_sources = 8;
723 break;
724 default:
725 dev_err(state->dev, "unknown MPP type 0x%x at 0x%x\n",
726 subtype, pad->base);
727 return -ENODEV;
728 }
729
730 val = pmic_mpp_read(state, pad, PMIC_MPP_REG_MODE_CTL);
731 if (val < 0)
732 return val;
733
734 pad->out_value = val & PMIC_MPP_REG_MODE_VALUE_MASK;
735
736 dir = val >> PMIC_MPP_REG_MODE_DIR_SHIFT;
737 dir &= PMIC_MPP_REG_MODE_DIR_MASK;
738
739 switch (dir) {
Bjorn Anderssoneaaf5dd2015-06-17 23:47:27 -0700740 case PMIC_MPP_MODE_DIGITAL_INPUT:
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300741 pad->input_enabled = true;
742 pad->output_enabled = false;
Bjorn Anderssoneb5c1442015-06-17 23:47:30 -0700743 pad->function = PMIC_MPP_DIGITAL;
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300744 break;
Bjorn Anderssoneaaf5dd2015-06-17 23:47:27 -0700745 case PMIC_MPP_MODE_DIGITAL_OUTPUT:
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300746 pad->input_enabled = false;
747 pad->output_enabled = true;
Bjorn Anderssoneb5c1442015-06-17 23:47:30 -0700748 pad->function = PMIC_MPP_DIGITAL;
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300749 break;
Bjorn Anderssoneaaf5dd2015-06-17 23:47:27 -0700750 case PMIC_MPP_MODE_DIGITAL_BIDIR:
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300751 pad->input_enabled = true;
752 pad->output_enabled = true;
Bjorn Anderssoneb5c1442015-06-17 23:47:30 -0700753 pad->function = PMIC_MPP_DIGITAL;
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300754 break;
Bjorn Anderssoneaaf5dd2015-06-17 23:47:27 -0700755 case PMIC_MPP_MODE_ANALOG_BIDIR:
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300756 pad->input_enabled = true;
757 pad->output_enabled = true;
Bjorn Anderssoneb5c1442015-06-17 23:47:30 -0700758 pad->function = PMIC_MPP_ANALOG;
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300759 break;
Bjorn Anderssoneaaf5dd2015-06-17 23:47:27 -0700760 case PMIC_MPP_MODE_ANALOG_INPUT:
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300761 pad->input_enabled = true;
762 pad->output_enabled = false;
Bjorn Anderssoneb5c1442015-06-17 23:47:30 -0700763 pad->function = PMIC_MPP_ANALOG;
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300764 break;
Bjorn Anderssoneaaf5dd2015-06-17 23:47:27 -0700765 case PMIC_MPP_MODE_ANALOG_OUTPUT:
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300766 pad->input_enabled = false;
767 pad->output_enabled = true;
Bjorn Anderssoneb5c1442015-06-17 23:47:30 -0700768 pad->function = PMIC_MPP_ANALOG;
Bjorn Andersson0e948042015-06-17 23:47:28 -0700769 break;
770 case PMIC_MPP_MODE_CURRENT_SINK:
771 pad->input_enabled = false;
772 pad->output_enabled = true;
Bjorn Anderssoneb5c1442015-06-17 23:47:30 -0700773 pad->function = PMIC_MPP_SINK;
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300774 break;
775 default:
776 dev_err(state->dev, "unknown MPP direction\n");
777 return -ENODEV;
778 }
779
Bjorn Anderssoneb5c1442015-06-17 23:47:30 -0700780 sel = val >> PMIC_MPP_REG_MODE_FUNCTION_SHIFT;
781 sel &= PMIC_MPP_REG_MODE_FUNCTION_MASK;
782
783 if (sel >= PMIC_MPP_SELECTOR_DTEST_FIRST)
Fenglin Wu9a508e82016-08-18 10:54:00 +0800784 pad->dtest = sel - PMIC_MPP_SELECTOR_DTEST_FIRST + 1;
Bjorn Anderssoneb5c1442015-06-17 23:47:30 -0700785 else if (sel == PMIC_MPP_SELECTOR_PAIRED)
786 pad->paired = true;
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300787
788 val = pmic_mpp_read(state, pad, PMIC_MPP_REG_DIG_VIN_CTL);
789 if (val < 0)
790 return val;
791
792 pad->power_source = val >> PMIC_MPP_REG_VIN_SHIFT;
793 pad->power_source &= PMIC_MPP_REG_VIN_MASK;
794
Fenglin Wu9651d642016-07-19 17:03:42 +0800795 val = pmic_mpp_read(state, pad, PMIC_MPP_REG_DIG_IN_CTL);
796 if (val < 0)
797 return val;
798
799 pad->dtest_buffer = val;
800
Stephen Boydae6d54f2016-01-08 12:25:53 -0800801 if (subtype != PMIC_MPP_SUBTYPE_ULT_4CH_NO_ANA_OUT &&
802 subtype != PMIC_MPP_SUBTYPE_ULT_4CH_NO_SINK) {
803 val = pmic_mpp_read(state, pad, PMIC_MPP_REG_DIG_PULL_CTL);
804 if (val < 0)
805 return val;
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300806
Stephen Boydae6d54f2016-01-08 12:25:53 -0800807 pad->pullup = val >> PMIC_MPP_REG_PULL_SHIFT;
808 pad->pullup &= PMIC_MPP_REG_PULL_MASK;
809 pad->has_pullup = true;
810 }
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300811
812 val = pmic_mpp_read(state, pad, PMIC_MPP_REG_AIN_CTL);
813 if (val < 0)
814 return val;
815
816 pad->amux_input = val >> PMIC_MPP_REG_AIN_ROUTE_SHIFT;
817 pad->amux_input &= PMIC_MPP_REG_AIN_ROUTE_MASK;
818
Bjorn Andersson0e948042015-06-17 23:47:28 -0700819 val = pmic_mpp_read(state, pad, PMIC_MPP_REG_SINK_CTL);
820 if (val < 0)
821 return val;
822
Fenglin Wu0e407372016-07-19 13:25:15 +0800823 val &= PMIC_MPP_REG_CURRENT_SINK_MASK;
824 pad->drive_strength = (val + 1) * MPP_CURRENT_SINK_MA_STEP_SIZE;
Bjorn Andersson0e948042015-06-17 23:47:28 -0700825
Bjorn Andersson099f3e42015-07-14 23:40:33 -0700826 val = pmic_mpp_read(state, pad, PMIC_MPP_REG_AOUT_CTL);
827 if (val < 0)
828 return val;
829
830 pad->aout_level = val;
831
Bjorn Andersson7682b372015-06-17 23:47:26 -0700832 val = pmic_mpp_read(state, pad, PMIC_MPP_REG_EN_CTL);
833 if (val < 0)
834 return val;
835
836 pad->is_enabled = !!val;
837
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300838 return 0;
839}
840
841static int pmic_mpp_probe(struct platform_device *pdev)
842{
843 struct device *dev = &pdev->dev;
844 struct pinctrl_pin_desc *pindesc;
845 struct pinctrl_desc *pctrldesc;
846 struct pmic_mpp_pad *pad, *pads;
847 struct pmic_mpp_state *state;
848 int ret, npins, i;
Stephen Boydab4256c2015-11-18 11:33:17 -0800849 u32 reg;
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300850
Stephen Boydab4256c2015-11-18 11:33:17 -0800851 ret = of_property_read_u32(dev->of_node, "reg", &reg);
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300852 if (ret < 0) {
Stephen Boydab4256c2015-11-18 11:33:17 -0800853 dev_err(dev, "missing base address");
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300854 return ret;
855 }
856
Stephen Boyda5ea13f2016-01-06 17:37:41 -0800857 npins = platform_irq_count(pdev);
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300858 if (!npins)
859 return -EINVAL;
Stephen Boyda5ea13f2016-01-06 17:37:41 -0800860 if (npins < 0)
861 return npins;
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300862
863 BUG_ON(npins > ARRAY_SIZE(pmic_mpp_groups));
864
865 state = devm_kzalloc(dev, sizeof(*state), GFP_KERNEL);
866 if (!state)
867 return -ENOMEM;
868
869 platform_set_drvdata(pdev, state);
870
871 state->dev = &pdev->dev;
872 state->map = dev_get_regmap(dev->parent, NULL);
873
874 pindesc = devm_kcalloc(dev, npins, sizeof(*pindesc), GFP_KERNEL);
875 if (!pindesc)
876 return -ENOMEM;
877
878 pads = devm_kcalloc(dev, npins, sizeof(*pads), GFP_KERNEL);
879 if (!pads)
880 return -ENOMEM;
881
882 pctrldesc = devm_kzalloc(dev, sizeof(*pctrldesc), GFP_KERNEL);
883 if (!pctrldesc)
884 return -ENOMEM;
885
886 pctrldesc->pctlops = &pmic_mpp_pinctrl_ops;
887 pctrldesc->pmxops = &pmic_mpp_pinmux_ops;
888 pctrldesc->confops = &pmic_mpp_pinconf_ops;
889 pctrldesc->owner = THIS_MODULE;
890 pctrldesc->name = dev_name(dev);
891 pctrldesc->pins = pindesc;
892 pctrldesc->npins = npins;
893
Bjorn Anderssonba5f94c2015-06-17 23:47:25 -0700894 pctrldesc->num_custom_params = ARRAY_SIZE(pmic_mpp_bindings);
895 pctrldesc->custom_params = pmic_mpp_bindings;
896#ifdef CONFIG_DEBUG_FS
897 pctrldesc->custom_conf_items = pmic_conf_items;
898#endif
899
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300900 for (i = 0; i < npins; i++, pindesc++) {
901 pad = &pads[i];
902 pindesc->drv_data = pad;
903 pindesc->number = i;
904 pindesc->name = pmic_mpp_groups[i];
905
906 pad->irq = platform_get_irq(pdev, i);
907 if (pad->irq < 0)
908 return pad->irq;
909
Stephen Boydab4256c2015-11-18 11:33:17 -0800910 pad->base = reg + i * PMIC_MPP_ADDRESS_RANGE;
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300911
912 ret = pmic_mpp_populate(state, pad);
913 if (ret < 0)
914 return ret;
915 }
916
917 state->chip = pmic_mpp_gpio_template;
Linus Walleij58383c72015-11-04 09:56:26 +0100918 state->chip.parent = dev;
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300919 state->chip.base = -1;
920 state->chip.ngpio = npins;
921 state->chip.label = dev_name(dev);
922 state->chip.of_gpio_n_cells = 2;
923 state->chip.can_sleep = false;
924
Laxman Dewangance18e592016-02-24 14:44:07 +0530925 state->ctrl = devm_pinctrl_register(dev, pctrldesc, state);
Masahiro Yamada323de9e2015-06-09 13:01:16 +0900926 if (IS_ERR(state->ctrl))
927 return PTR_ERR(state->ctrl);
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300928
Linus Walleij064761d2015-12-08 09:53:39 +0100929 ret = gpiochip_add_data(&state->chip, state);
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300930 if (ret) {
931 dev_err(state->dev, "can't add gpio chip\n");
Laxman Dewangance18e592016-02-24 14:44:07 +0530932 return ret;
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300933 }
934
935 ret = gpiochip_add_pin_range(&state->chip, dev_name(dev), 0, 0, npins);
936 if (ret) {
937 dev_err(dev, "failed to add pin range\n");
938 goto err_range;
939 }
940
941 return 0;
942
943err_range:
944 gpiochip_remove(&state->chip);
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300945 return ret;
946}
947
948static int pmic_mpp_remove(struct platform_device *pdev)
949{
950 struct pmic_mpp_state *state = platform_get_drvdata(pdev);
951
952 gpiochip_remove(&state->chip);
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300953 return 0;
954}
955
956static const struct of_device_id pmic_mpp_of_match[] = {
957 { .compatible = "qcom,pm8841-mpp" }, /* 4 MPP's */
Ivan T. Ivanov7414b092015-03-31 12:37:18 +0300958 { .compatible = "qcom,pm8916-mpp" }, /* 4 MPP's */
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300959 { .compatible = "qcom,pm8941-mpp" }, /* 8 MPP's */
Stephen Boydeb9610f2015-11-17 16:52:33 -0800960 { .compatible = "qcom,pm8994-mpp" }, /* 8 MPP's */
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300961 { .compatible = "qcom,pma8084-mpp" }, /* 8 MPP's */
Stephen Boyd94fa6672016-07-11 12:01:09 -0700962 { .compatible = "qcom,spmi-mpp" }, /* Generic */
Ivan T. Ivanovcfb24f62014-10-22 12:58:47 +0300963 { },
964};
965
966MODULE_DEVICE_TABLE(of, pmic_mpp_of_match);
967
968static struct platform_driver pmic_mpp_driver = {
969 .driver = {
970 .name = "qcom-spmi-mpp",
971 .of_match_table = pmic_mpp_of_match,
972 },
973 .probe = pmic_mpp_probe,
974 .remove = pmic_mpp_remove,
975};
976
977module_platform_driver(pmic_mpp_driver);
978
979MODULE_AUTHOR("Ivan T. Ivanov <iivanov@mm-sol.com>");
980MODULE_DESCRIPTION("Qualcomm SPMI PMIC MPP pin control driver");
981MODULE_ALIAS("platform:qcom-spmi-mpp");
982MODULE_LICENSE("GPL v2");