Blame view

drivers/pinctrl/pinctrl-sx150x.c 29.1 KB
97fb5e8d9   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
2
3
4
5
6
7
8
  /*
   * Copyright (c) 2016, BayLibre, SAS. All rights reserved.
   * Author: Neil Armstrong <narmstrong@baylibre.com>
   *
   * Copyright (c) 2010, Code Aurora Forum. All rights reserved.
   *
   * Driver for Semtech SX150X I2C GPIO Expanders
6c4ef627d   Peter Rosin   pinctrl: sx150x: ...
9
   * The handling of the 4-bit chips (SX1501/SX1504/SX1507) is untested.
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
10
11
   *
   * Author: Gregory Bean <gbean@codeaurora.org>
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
12
   */
0db0f26c2   Andrey Smirnov   pinctrl-sx150x: C...
13
  #include <linux/regmap.h>
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
14
15
16
17
18
19
20
  #include <linux/i2c.h>
  #include <linux/init.h>
  #include <linux/interrupt.h>
  #include <linux/irq.h>
  #include <linux/mutex.h>
  #include <linux/slab.h>
  #include <linux/of.h>
e3ba81206   Andrey Smirnov   pinctrl-sx150x: I...
21
  #include <linux/of_device.h>
fc669d13c   Linus Walleij   pinctrl: sx150x: ...
22
  #include <linux/gpio/driver.h>
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  #include <linux/pinctrl/pinconf.h>
  #include <linux/pinctrl/pinctrl.h>
  #include <linux/pinctrl/pinmux.h>
  #include <linux/pinctrl/pinconf-generic.h>
  
  #include "core.h"
  #include "pinconf.h"
  #include "pinctrl-utils.h"
  
  /* The chip models of sx150x */
  enum {
  	SX150X_123 = 0,
  	SX150X_456,
  	SX150X_789,
  };
7d68a79ac   Andrey Smirnov   pinctrl-sx150x: R...
38
39
  enum {
  	SX150X_789_REG_MISC_AUTOCLEAR_OFF = 1 << 0,
0db0f26c2   Andrey Smirnov   pinctrl-sx150x: C...
40
  	SX150X_MAX_REGISTER = 0xad,
fd931f237   Andrey Smirnov   pinctrl-sx150x: R...
41
42
  	SX150X_IRQ_TYPE_EDGE_RISING = 0x1,
  	SX150X_IRQ_TYPE_EDGE_FALLING = 0x2,
f9038d603   Andrey Smirnov   pinctrl-sx150x: R...
43
44
  	SX150X_789_RESET_KEY1 = 0x12,
  	SX150X_789_RESET_KEY2 = 0x34,
7d68a79ac   Andrey Smirnov   pinctrl-sx150x: R...
45
  };
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
46
47
48
49
50
51
52
53
  
  struct sx150x_123_pri {
  	u8 reg_pld_mode;
  	u8 reg_pld_table0;
  	u8 reg_pld_table1;
  	u8 reg_pld_table2;
  	u8 reg_pld_table3;
  	u8 reg_pld_table4;
c9d26f1aa   Peter Rosin   pinctrl: sx150x: ...
54
  	u8 reg_advanced;
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
55
56
57
58
59
60
61
62
63
  };
  
  struct sx150x_456_pri {
  	u8 reg_pld_mode;
  	u8 reg_pld_table0;
  	u8 reg_pld_table1;
  	u8 reg_pld_table2;
  	u8 reg_pld_table3;
  	u8 reg_pld_table4;
c9d26f1aa   Peter Rosin   pinctrl: sx150x: ...
64
  	u8 reg_advanced;
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  };
  
  struct sx150x_789_pri {
  	u8 reg_drain;
  	u8 reg_polarity;
  	u8 reg_clock;
  	u8 reg_misc;
  	u8 reg_reset;
  	u8 ngpios;
  };
  
  struct sx150x_device_data {
  	u8 model;
  	u8 reg_pullup;
  	u8 reg_pulldn;
  	u8 reg_dir;
  	u8 reg_data;
  	u8 reg_irq_mask;
  	u8 reg_irq_src;
  	u8 reg_sense;
  	u8 ngpios;
  	union {
  		struct sx150x_123_pri x123;
  		struct sx150x_456_pri x456;
  		struct sx150x_789_pri x789;
  	} pri;
  	const struct pinctrl_pin_desc *pins;
  	unsigned int npins;
  };
  
  struct sx150x_pinctrl {
  	struct device *dev;
  	struct i2c_client *client;
  	struct pinctrl_dev *pctldev;
  	struct pinctrl_desc pinctrl_desc;
  	struct gpio_chip gpio;
  	struct irq_chip irq_chip;
0db0f26c2   Andrey Smirnov   pinctrl-sx150x: C...
102
  	struct regmap *regmap;
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
103
  	struct {
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
104
105
  		u32 sense;
  		u32 masked;
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
106
107
108
109
  	} irq;
  	struct mutex lock;
  	const struct sx150x_device_data *data;
  };
4f5ac8cf0   Peter Rosin   pinctrl: sx150x: ...
110
111
112
113
114
115
116
  static const struct pinctrl_pin_desc sx150x_4_pins[] = {
  	PINCTRL_PIN(0, "gpio0"),
  	PINCTRL_PIN(1, "gpio1"),
  	PINCTRL_PIN(2, "gpio2"),
  	PINCTRL_PIN(3, "gpio3"),
  	PINCTRL_PIN(4, "oscio"),
  };
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
  static const struct pinctrl_pin_desc sx150x_8_pins[] = {
  	PINCTRL_PIN(0, "gpio0"),
  	PINCTRL_PIN(1, "gpio1"),
  	PINCTRL_PIN(2, "gpio2"),
  	PINCTRL_PIN(3, "gpio3"),
  	PINCTRL_PIN(4, "gpio4"),
  	PINCTRL_PIN(5, "gpio5"),
  	PINCTRL_PIN(6, "gpio6"),
  	PINCTRL_PIN(7, "gpio7"),
  	PINCTRL_PIN(8, "oscio"),
  };
  
  static const struct pinctrl_pin_desc sx150x_16_pins[] = {
  	PINCTRL_PIN(0, "gpio0"),
  	PINCTRL_PIN(1, "gpio1"),
  	PINCTRL_PIN(2, "gpio2"),
  	PINCTRL_PIN(3, "gpio3"),
  	PINCTRL_PIN(4, "gpio4"),
  	PINCTRL_PIN(5, "gpio5"),
  	PINCTRL_PIN(6, "gpio6"),
  	PINCTRL_PIN(7, "gpio7"),
  	PINCTRL_PIN(8, "gpio8"),
  	PINCTRL_PIN(9, "gpio9"),
  	PINCTRL_PIN(10, "gpio10"),
  	PINCTRL_PIN(11, "gpio11"),
  	PINCTRL_PIN(12, "gpio12"),
  	PINCTRL_PIN(13, "gpio13"),
  	PINCTRL_PIN(14, "gpio14"),
  	PINCTRL_PIN(15, "gpio15"),
  	PINCTRL_PIN(16, "oscio"),
  };
4f5ac8cf0   Peter Rosin   pinctrl: sx150x: ...
148
149
150
151
152
153
154
155
156
157
158
159
160
  static const struct sx150x_device_data sx1501q_device_data = {
  	.model = SX150X_123,
  	.reg_pullup	= 0x02,
  	.reg_pulldn	= 0x03,
  	.reg_dir	= 0x01,
  	.reg_data	= 0x00,
  	.reg_irq_mask	= 0x05,
  	.reg_irq_src	= 0x08,
  	.reg_sense	= 0x07,
  	.pri.x123 = {
  		.reg_pld_mode	= 0x10,
  		.reg_pld_table0	= 0x11,
  		.reg_pld_table2	= 0x13,
c9d26f1aa   Peter Rosin   pinctrl: sx150x: ...
161
  		.reg_advanced	= 0xad,
4f5ac8cf0   Peter Rosin   pinctrl: sx150x: ...
162
163
164
165
166
  	},
  	.ngpios	= 4,
  	.pins = sx150x_4_pins,
  	.npins = 4, /* oscio not available */
  };
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
167
168
169
170
171
172
173
174
  static const struct sx150x_device_data sx1502q_device_data = {
  	.model = SX150X_123,
  	.reg_pullup	= 0x02,
  	.reg_pulldn	= 0x03,
  	.reg_dir	= 0x01,
  	.reg_data	= 0x00,
  	.reg_irq_mask	= 0x05,
  	.reg_irq_src	= 0x08,
1663682cf   Peter Rosin   pinctrl: sx150x: ...
175
  	.reg_sense	= 0x06,
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
176
177
178
179
180
181
182
  	.pri.x123 = {
  		.reg_pld_mode	= 0x10,
  		.reg_pld_table0	= 0x11,
  		.reg_pld_table1	= 0x12,
  		.reg_pld_table2	= 0x13,
  		.reg_pld_table3	= 0x14,
  		.reg_pld_table4	= 0x15,
c9d26f1aa   Peter Rosin   pinctrl: sx150x: ...
183
  		.reg_advanced	= 0xad,
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
184
185
186
187
188
  	},
  	.ngpios	= 8,
  	.pins = sx150x_8_pins,
  	.npins = 8, /* oscio not available */
  };
6697546d6   Andrey Smirnov   pinctrl-sx150x: A...
189
190
  static const struct sx150x_device_data sx1503q_device_data = {
  	.model = SX150X_123,
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
191
192
193
194
195
196
197
  	.reg_pullup	= 0x04,
  	.reg_pulldn	= 0x06,
  	.reg_dir	= 0x02,
  	.reg_data	= 0x00,
  	.reg_irq_mask	= 0x08,
  	.reg_irq_src	= 0x0e,
  	.reg_sense	= 0x0a,
6697546d6   Andrey Smirnov   pinctrl-sx150x: A...
198
  	.pri.x123 = {
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
199
200
201
202
203
204
  		.reg_pld_mode	= 0x20,
  		.reg_pld_table0	= 0x22,
  		.reg_pld_table1	= 0x24,
  		.reg_pld_table2	= 0x26,
  		.reg_pld_table3	= 0x28,
  		.reg_pld_table4	= 0x2a,
c9d26f1aa   Peter Rosin   pinctrl: sx150x: ...
205
  		.reg_advanced	= 0xad,
6697546d6   Andrey Smirnov   pinctrl-sx150x: A...
206
207
208
209
210
  	},
  	.ngpios	= 16,
  	.pins = sx150x_16_pins,
  	.npins  = 16, /* oscio not available */
  };
4f5ac8cf0   Peter Rosin   pinctrl: sx150x: ...
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
  static const struct sx150x_device_data sx1504q_device_data = {
  	.model = SX150X_456,
  	.reg_pullup	= 0x02,
  	.reg_pulldn	= 0x03,
  	.reg_dir	= 0x01,
  	.reg_data	= 0x00,
  	.reg_irq_mask	= 0x05,
  	.reg_irq_src	= 0x08,
  	.reg_sense	= 0x07,
  	.pri.x456 = {
  		.reg_pld_mode	= 0x10,
  		.reg_pld_table0	= 0x11,
  		.reg_pld_table2	= 0x13,
  	},
  	.ngpios	= 4,
  	.pins = sx150x_4_pins,
  	.npins = 4, /* oscio not available */
  };
  
  static const struct sx150x_device_data sx1505q_device_data = {
  	.model = SX150X_456,
  	.reg_pullup	= 0x02,
  	.reg_pulldn	= 0x03,
  	.reg_dir	= 0x01,
  	.reg_data	= 0x00,
  	.reg_irq_mask	= 0x05,
  	.reg_irq_src	= 0x08,
  	.reg_sense	= 0x06,
  	.pri.x456 = {
  		.reg_pld_mode	= 0x10,
  		.reg_pld_table0	= 0x11,
  		.reg_pld_table1	= 0x12,
  		.reg_pld_table2	= 0x13,
  		.reg_pld_table3	= 0x14,
  		.reg_pld_table4	= 0x15,
  	},
  	.ngpios	= 8,
  	.pins = sx150x_8_pins,
  	.npins = 8, /* oscio not available */
  };
bba709bd7   Peter Rosin   pinctrl: sx150x: ...
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
  static const struct sx150x_device_data sx1506q_device_data = {
  	.model = SX150X_456,
  	.reg_pullup	= 0x04,
  	.reg_pulldn	= 0x06,
  	.reg_dir	= 0x02,
  	.reg_data	= 0x00,
  	.reg_irq_mask	= 0x08,
  	.reg_irq_src	= 0x0e,
  	.reg_sense	= 0x0a,
  	.pri.x456 = {
  		.reg_pld_mode	= 0x20,
  		.reg_pld_table0	= 0x22,
  		.reg_pld_table1	= 0x24,
  		.reg_pld_table2	= 0x26,
  		.reg_pld_table3	= 0x28,
  		.reg_pld_table4	= 0x2a,
c9d26f1aa   Peter Rosin   pinctrl: sx150x: ...
267
  		.reg_advanced	= 0xad,
bba709bd7   Peter Rosin   pinctrl: sx150x: ...
268
269
270
271
272
  	},
  	.ngpios	= 16,
  	.pins = sx150x_16_pins,
  	.npins = 16, /* oscio not available */
  };
4f5ac8cf0   Peter Rosin   pinctrl: sx150x: ...
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
  static const struct sx150x_device_data sx1507q_device_data = {
  	.model = SX150X_789,
  	.reg_pullup	= 0x03,
  	.reg_pulldn	= 0x04,
  	.reg_dir	= 0x07,
  	.reg_data	= 0x08,
  	.reg_irq_mask	= 0x09,
  	.reg_irq_src	= 0x0b,
  	.reg_sense	= 0x0a,
  	.pri.x789 = {
  		.reg_drain	= 0x05,
  		.reg_polarity	= 0x06,
  		.reg_clock	= 0x0d,
  		.reg_misc	= 0x0e,
  		.reg_reset	= 0x7d,
  	},
  	.ngpios = 4,
  	.pins = sx150x_4_pins,
  	.npins = ARRAY_SIZE(sx150x_4_pins),
  };
bba709bd7   Peter Rosin   pinctrl: sx150x: ...
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
  static const struct sx150x_device_data sx1508q_device_data = {
  	.model = SX150X_789,
  	.reg_pullup	= 0x03,
  	.reg_pulldn	= 0x04,
  	.reg_dir	= 0x07,
  	.reg_data	= 0x08,
  	.reg_irq_mask	= 0x09,
  	.reg_irq_src	= 0x0c,
  	.reg_sense	= 0x0a,
  	.pri.x789 = {
  		.reg_drain	= 0x05,
  		.reg_polarity	= 0x06,
  		.reg_clock	= 0x0f,
  		.reg_misc	= 0x10,
  		.reg_reset	= 0x7d,
  	},
  	.ngpios = 8,
  	.pins = sx150x_8_pins,
  	.npins = ARRAY_SIZE(sx150x_8_pins),
  };
  
  static const struct sx150x_device_data sx1509q_device_data = {
  	.model = SX150X_789,
  	.reg_pullup	= 0x06,
  	.reg_pulldn	= 0x08,
  	.reg_dir	= 0x0e,
  	.reg_data	= 0x10,
  	.reg_irq_mask	= 0x12,
  	.reg_irq_src	= 0x18,
  	.reg_sense	= 0x14,
  	.pri.x789 = {
  		.reg_drain	= 0x0a,
  		.reg_polarity	= 0x0c,
  		.reg_clock	= 0x1e,
  		.reg_misc	= 0x1f,
  		.reg_reset	= 0x7d,
  	},
  	.ngpios	= 16,
  	.pins = sx150x_16_pins,
  	.npins = ARRAY_SIZE(sx150x_16_pins),
  };
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
  static int sx150x_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
  {
  	return 0;
  }
  
  static const char *sx150x_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
  						unsigned int group)
  {
  	return NULL;
  }
  
  static int sx150x_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
  					unsigned int group,
  					const unsigned int **pins,
  					unsigned int *num_pins)
  {
  	return -ENOTSUPP;
  }
  
  static const struct pinctrl_ops sx150x_pinctrl_ops = {
  	.get_groups_count = sx150x_pinctrl_get_groups_count,
  	.get_group_name = sx150x_pinctrl_get_group_name,
  	.get_group_pins = sx150x_pinctrl_get_group_pins,
  #ifdef CONFIG_OF
  	.dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
  	.dt_free_map = pinctrl_utils_free_map,
  #endif
  };
  
  static bool sx150x_pin_is_oscio(struct sx150x_pinctrl *pctl, unsigned int pin)
  {
  	if (pin >= pctl->data->npins)
  		return false;
  
  	/* OSCIO pin is only present in 789 devices */
  	if (pctl->data->model != SX150X_789)
  		return false;
  
  	return !strcmp(pctl->data->pins[pin].name, "oscio");
  }
  
  static int sx150x_gpio_get_direction(struct gpio_chip *chip,
  				      unsigned int offset)
  {
  	struct sx150x_pinctrl *pctl = gpiochip_get_data(chip);
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
379
380
  	unsigned int value;
  	int ret;
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
381
382
  
  	if (sx150x_pin_is_oscio(pctl, offset))
3c8278735   Matti Vaittinen   pinctrl: Use new ...
383
  		return GPIO_LINE_DIRECTION_OUT;
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
384

6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
385
386
387
  	ret = regmap_read(pctl->regmap, pctl->data->reg_dir, &value);
  	if (ret < 0)
  		return ret;
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
388

3c8278735   Matti Vaittinen   pinctrl: Use new ...
389
390
391
392
  	if (value & BIT(offset))
  		return GPIO_LINE_DIRECTION_IN;
  
  	return GPIO_LINE_DIRECTION_OUT;
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
393
394
395
396
397
  }
  
  static int sx150x_gpio_get(struct gpio_chip *chip, unsigned int offset)
  {
  	struct sx150x_pinctrl *pctl = gpiochip_get_data(chip);
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
398
399
  	unsigned int value;
  	int ret;
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
400
401
402
  
  	if (sx150x_pin_is_oscio(pctl, offset))
  		return -EINVAL;
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
403
404
405
  	ret = regmap_read(pctl->regmap, pctl->data->reg_data, &value);
  	if (ret < 0)
  		return ret;
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
406

6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
407
  	return !!(value & BIT(offset));
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
408
  }
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
409
410
411
412
413
414
  static int __sx150x_gpio_set(struct sx150x_pinctrl *pctl, unsigned int offset,
  			     int value)
  {
  	return regmap_write_bits(pctl->regmap, pctl->data->reg_data,
  				 BIT(offset), value ? BIT(offset) : 0);
  }
ab5bd0354   Andrey Smirnov   pinctrl-sx150x: I...
415
416
417
418
419
420
421
  static int sx150x_gpio_oscio_set(struct sx150x_pinctrl *pctl,
  				 int value)
  {
  	return regmap_write(pctl->regmap,
  			    pctl->data->pri.x789.reg_clock,
  			    (value ? 0x1f : 0x10));
  }
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
422
  static void sx150x_gpio_set(struct gpio_chip *chip, unsigned int offset,
7bd474963   Peter Rosin   pinctrl: sx150x: ...
423
  			    int value)
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
424
425
  {
  	struct sx150x_pinctrl *pctl = gpiochip_get_data(chip);
d977a876c   Andrey Smirnov   pinctrl-sx150x: R...
426
  	if (sx150x_pin_is_oscio(pctl, offset))
ab5bd0354   Andrey Smirnov   pinctrl-sx150x: I...
427
  		sx150x_gpio_oscio_set(pctl, value);
d977a876c   Andrey Smirnov   pinctrl-sx150x: R...
428
  	else
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
429
  		__sx150x_gpio_set(pctl, offset, value);
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
430
  }
ec61168bc   Peter Rosin   pinctrl: sx150x: ...
431
432
433
434
435
436
437
438
  static void sx150x_gpio_set_multiple(struct gpio_chip *chip,
  				     unsigned long *mask,
  				     unsigned long *bits)
  {
  	struct sx150x_pinctrl *pctl = gpiochip_get_data(chip);
  
  	regmap_write_bits(pctl->regmap, pctl->data->reg_data, *mask, *bits);
  }
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
439
  static int sx150x_gpio_direction_input(struct gpio_chip *chip,
7bd474963   Peter Rosin   pinctrl: sx150x: ...
440
  				       unsigned int offset)
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
441
442
  {
  	struct sx150x_pinctrl *pctl = gpiochip_get_data(chip);
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
443
444
445
  
  	if (sx150x_pin_is_oscio(pctl, offset))
  		return -EINVAL;
d977a876c   Andrey Smirnov   pinctrl-sx150x: R...
446
447
448
  	return regmap_write_bits(pctl->regmap,
  				 pctl->data->reg_dir,
  				 BIT(offset), BIT(offset));
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
449
450
451
  }
  
  static int sx150x_gpio_direction_output(struct gpio_chip *chip,
7bd474963   Peter Rosin   pinctrl: sx150x: ...
452
  					unsigned int offset, int value)
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
453
454
  {
  	struct sx150x_pinctrl *pctl = gpiochip_get_data(chip);
d977a876c   Andrey Smirnov   pinctrl-sx150x: R...
455
  	int ret;
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
456

ab5bd0354   Andrey Smirnov   pinctrl-sx150x: I...
457
458
  	if (sx150x_pin_is_oscio(pctl, offset))
  		return sx150x_gpio_oscio_set(pctl, value);
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
459

d977a876c   Andrey Smirnov   pinctrl-sx150x: R...
460
461
462
  	ret = __sx150x_gpio_set(pctl, offset, value);
  	if (ret < 0)
  		return ret;
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
463

d977a876c   Andrey Smirnov   pinctrl-sx150x: R...
464
465
466
  	return regmap_write_bits(pctl->regmap,
  				 pctl->data->reg_dir,
  				 BIT(offset), 0);
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
467
468
469
470
471
472
473
  }
  
  static void sx150x_irq_mask(struct irq_data *d)
  {
  	struct sx150x_pinctrl *pctl =
  			gpiochip_get_data(irq_data_get_irq_chip_data(d));
  	unsigned int n = d->hwirq;
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
474
  	pctl->irq.masked |= BIT(n);
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
475
476
477
478
479
480
481
  }
  
  static void sx150x_irq_unmask(struct irq_data *d)
  {
  	struct sx150x_pinctrl *pctl =
  			gpiochip_get_data(irq_data_get_irq_chip_data(d));
  	unsigned int n = d->hwirq;
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
482
  	pctl->irq.masked &= ~BIT(n);
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
483
  }
fd931f237   Andrey Smirnov   pinctrl-sx150x: R...
484
485
486
487
488
489
490
491
492
493
494
495
496
497
  static void sx150x_irq_set_sense(struct sx150x_pinctrl *pctl,
  				 unsigned int line, unsigned int sense)
  {
  	/*
  	 * Every interrupt line is represented by two bits shifted
  	 * proportionally to the line number
  	 */
  	const unsigned int n = line * 2;
  	const unsigned int mask = ~((SX150X_IRQ_TYPE_EDGE_RISING |
  				     SX150X_IRQ_TYPE_EDGE_FALLING) << n);
  
  	pctl->irq.sense &= mask;
  	pctl->irq.sense |= sense << n;
  }
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
498
499
500
501
502
503
504
505
506
507
508
509
  static int sx150x_irq_set_type(struct irq_data *d, unsigned int flow_type)
  {
  	struct sx150x_pinctrl *pctl =
  			gpiochip_get_data(irq_data_get_irq_chip_data(d));
  	unsigned int n, val = 0;
  
  	if (flow_type & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW))
  		return -EINVAL;
  
  	n = d->hwirq;
  
  	if (flow_type & IRQ_TYPE_EDGE_RISING)
fd931f237   Andrey Smirnov   pinctrl-sx150x: R...
510
  		val |= SX150X_IRQ_TYPE_EDGE_RISING;
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
511
  	if (flow_type & IRQ_TYPE_EDGE_FALLING)
fd931f237   Andrey Smirnov   pinctrl-sx150x: R...
512
  		val |= SX150X_IRQ_TYPE_EDGE_FALLING;
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
513

fd931f237   Andrey Smirnov   pinctrl-sx150x: R...
514
  	sx150x_irq_set_sense(pctl, n, val);
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
515
516
517
518
519
520
  	return 0;
  }
  
  static irqreturn_t sx150x_irq_thread_fn(int irq, void *dev_id)
  {
  	struct sx150x_pinctrl *pctl = (struct sx150x_pinctrl *)dev_id;
05a90cc72   Andrey Smirnov   pinctrl-sx150x: S...
521
  	unsigned long n, status;
0db0f26c2   Andrey Smirnov   pinctrl-sx150x: C...
522
  	unsigned int val;
05a90cc72   Andrey Smirnov   pinctrl-sx150x: S...
523
  	int err;
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
524

6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
525
526
527
  	err = regmap_read(pctl->regmap, pctl->data->reg_irq_src, &val);
  	if (err < 0)
  		return IRQ_NONE;
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
528

6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
529
530
531
  	err = regmap_write(pctl->regmap, pctl->data->reg_irq_src, val);
  	if (err < 0)
  		return IRQ_NONE;
05a90cc72   Andrey Smirnov   pinctrl-sx150x: S...
532
533
  	status = val;
  	for_each_set_bit(n, &status, pctl->data->ngpios)
f0fbe7bce   Thierry Reding   gpio: Move irqdom...
534
  		handle_nested_irq(irq_find_mapping(pctl->gpio.irq.domain, n));
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
535

05a90cc72   Andrey Smirnov   pinctrl-sx150x: S...
536
  	return IRQ_HANDLED;
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
537
538
539
540
541
542
543
544
545
546
547
548
549
550
  }
  
  static void sx150x_irq_bus_lock(struct irq_data *d)
  {
  	struct sx150x_pinctrl *pctl =
  			gpiochip_get_data(irq_data_get_irq_chip_data(d));
  
  	mutex_lock(&pctl->lock);
  }
  
  static void sx150x_irq_bus_sync_unlock(struct irq_data *d)
  {
  	struct sx150x_pinctrl *pctl =
  			gpiochip_get_data(irq_data_get_irq_chip_data(d));
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
551

6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
552
553
  	regmap_write(pctl->regmap, pctl->data->reg_irq_mask, pctl->irq.masked);
  	regmap_write(pctl->regmap, pctl->data->reg_sense, pctl->irq.sense);
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
554
555
556
557
558
559
560
561
562
563
  	mutex_unlock(&pctl->lock);
  }
  
  static int sx150x_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
  			      unsigned long *config)
  {
  	struct sx150x_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  	unsigned int param = pinconf_to_config_param(*config);
  	int ret;
  	u32 arg;
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
564
  	unsigned int data;
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
565
566
  
  	if (sx150x_pin_is_oscio(pctl, pin)) {
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
567
568
569
  		switch (param) {
  		case PIN_CONFIG_DRIVE_PUSH_PULL:
  		case PIN_CONFIG_OUTPUT:
0db0f26c2   Andrey Smirnov   pinctrl-sx150x: C...
570
571
572
  			ret = regmap_read(pctl->regmap,
  					  pctl->data->pri.x789.reg_clock,
  					  &data);
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
  			if (ret < 0)
  				return ret;
  
  			if (param == PIN_CONFIG_DRIVE_PUSH_PULL)
  				arg = (data & 0x1f) ? 1 : 0;
  			else {
  				if ((data & 0x1f) == 0x1f)
  					arg = 1;
  				else if ((data & 0x1f) == 0x10)
  					arg = 0;
  				else
  					return -EINVAL;
  			}
  
  			break;
  		default:
  			return -ENOTSUPP;
  		}
  
  		goto out;
  	}
  
  	switch (param) {
  	case PIN_CONFIG_BIAS_PULL_DOWN:
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
597
598
599
600
  		ret = regmap_read(pctl->regmap,
  				  pctl->data->reg_pulldn,
  				  &data);
  		data &= BIT(pin);
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
601
602
603
604
605
606
607
608
609
610
611
  
  		if (ret < 0)
  			return ret;
  
  		if (!ret)
  			return -EINVAL;
  
  		arg = 1;
  		break;
  
  	case PIN_CONFIG_BIAS_PULL_UP:
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
612
613
614
615
  		ret = regmap_read(pctl->regmap,
  				  pctl->data->reg_pullup,
  				  &data);
  		data &= BIT(pin);
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
616
617
618
619
620
621
622
623
624
625
626
627
628
  
  		if (ret < 0)
  			return ret;
  
  		if (!ret)
  			return -EINVAL;
  
  		arg = 1;
  		break;
  
  	case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  		if (pctl->data->model != SX150X_789)
  			return -ENOTSUPP;
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
629
630
631
632
  		ret = regmap_read(pctl->regmap,
  				  pctl->data->pri.x789.reg_drain,
  				  &data);
  		data &= BIT(pin);
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
633
634
635
  
  		if (ret < 0)
  			return ret;
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
636
  		if (!data)
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
637
638
639
640
641
642
643
644
645
  			return -EINVAL;
  
  		arg = 1;
  		break;
  
  	case PIN_CONFIG_DRIVE_PUSH_PULL:
  		if (pctl->data->model != SX150X_789)
  			arg = true;
  		else {
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
646
647
648
649
  			ret = regmap_read(pctl->regmap,
  					  pctl->data->pri.x789.reg_drain,
  					  &data);
  			data &= BIT(pin);
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
650
651
652
  
  			if (ret < 0)
  				return ret;
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
653
  			if (data)
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
654
655
656
657
658
659
660
661
662
663
  				return -EINVAL;
  
  			arg = 1;
  		}
  		break;
  
  	case PIN_CONFIG_OUTPUT:
  		ret = sx150x_gpio_get_direction(&pctl->gpio, pin);
  		if (ret < 0)
  			return ret;
3c8278735   Matti Vaittinen   pinctrl: Use new ...
664
  		if (ret == GPIO_LINE_DIRECTION_IN)
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
  			return -EINVAL;
  
  		ret = sx150x_gpio_get(&pctl->gpio, pin);
  		if (ret < 0)
  			return ret;
  
  		arg = ret;
  		break;
  
  	default:
  		return -ENOTSUPP;
  	}
  
  out:
  	*config = pinconf_to_config_packed(param, arg);
  
  	return 0;
  }
  
  static int sx150x_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
  			      unsigned long *configs, unsigned int num_configs)
  {
  	struct sx150x_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  	enum pin_config_param param;
  	u32 arg;
  	int i;
  	int ret;
  
  	for (i = 0; i < num_configs; i++) {
  		param = pinconf_to_config_param(configs[i]);
  		arg = pinconf_to_config_argument(configs[i]);
  
  		if (sx150x_pin_is_oscio(pctl, pin)) {
  			if (param == PIN_CONFIG_OUTPUT) {
  				ret = sx150x_gpio_direction_output(&pctl->gpio,
  								   pin, arg);
  				if (ret < 0)
  					return ret;
  
  				continue;
  			} else
  				return -ENOTSUPP;
  		}
  
  		switch (param) {
  		case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
  		case PIN_CONFIG_BIAS_DISABLE:
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
712
713
714
  			ret = regmap_write_bits(pctl->regmap,
  						pctl->data->reg_pulldn,
  						BIT(pin), 0);
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
715
716
  			if (ret < 0)
  				return ret;
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
717
718
719
  			ret = regmap_write_bits(pctl->regmap,
  						pctl->data->reg_pullup,
  						BIT(pin), 0);
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
720
721
722
723
724
725
  			if (ret < 0)
  				return ret;
  
  			break;
  
  		case PIN_CONFIG_BIAS_PULL_UP:
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
726
727
728
  			ret = regmap_write_bits(pctl->regmap,
  						pctl->data->reg_pullup,
  						BIT(pin), BIT(pin));
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
729
730
731
732
733
734
  			if (ret < 0)
  				return ret;
  
  			break;
  
  		case PIN_CONFIG_BIAS_PULL_DOWN:
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
735
736
737
  			ret = regmap_write_bits(pctl->regmap,
  						pctl->data->reg_pulldn,
  						BIT(pin), BIT(pin));
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
738
739
740
741
742
743
  			if (ret < 0)
  				return ret;
  
  			break;
  
  		case PIN_CONFIG_DRIVE_OPEN_DRAIN:
2956b5d94   Mika Westerberg   pinctrl / gpio: I...
744
745
746
747
748
749
750
  			if (pctl->data->model != SX150X_789 ||
  			    sx150x_pin_is_oscio(pctl, pin))
  				return -ENOTSUPP;
  
  			ret = regmap_write_bits(pctl->regmap,
  						pctl->data->pri.x789.reg_drain,
  						BIT(pin), BIT(pin));
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
751
752
753
754
755
756
  			if (ret < 0)
  				return ret;
  
  			break;
  
  		case PIN_CONFIG_DRIVE_PUSH_PULL:
2956b5d94   Mika Westerberg   pinctrl / gpio: I...
757
758
759
760
761
762
763
  			if (pctl->data->model != SX150X_789 ||
  			    sx150x_pin_is_oscio(pctl, pin))
  				return 0;
  
  			ret = regmap_write_bits(pctl->regmap,
  						pctl->data->pri.x789.reg_drain,
  						BIT(pin), 0);
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
  			if (ret < 0)
  				return ret;
  
  			break;
  
  		case PIN_CONFIG_OUTPUT:
  			ret = sx150x_gpio_direction_output(&pctl->gpio,
  							   pin, arg);
  			if (ret < 0)
  				return ret;
  
  			break;
  
  		default:
  			return -ENOTSUPP;
  		}
  	} /* for each config */
  
  	return 0;
  }
  
  static const struct pinconf_ops sx150x_pinconf_ops = {
  	.pin_config_get = sx150x_pinconf_get,
  	.pin_config_set = sx150x_pinconf_set,
  	.is_generic = true,
  };
  
  static const struct i2c_device_id sx150x_id[] = {
4f5ac8cf0   Peter Rosin   pinctrl: sx150x: ...
792
  	{"sx1501q", (kernel_ulong_t) &sx1501q_device_data },
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
793
  	{"sx1502q", (kernel_ulong_t) &sx1502q_device_data },
6697546d6   Andrey Smirnov   pinctrl-sx150x: A...
794
  	{"sx1503q", (kernel_ulong_t) &sx1503q_device_data },
4f5ac8cf0   Peter Rosin   pinctrl: sx150x: ...
795
796
  	{"sx1504q", (kernel_ulong_t) &sx1504q_device_data },
  	{"sx1505q", (kernel_ulong_t) &sx1505q_device_data },
bba709bd7   Peter Rosin   pinctrl: sx150x: ...
797
  	{"sx1506q", (kernel_ulong_t) &sx1506q_device_data },
4f5ac8cf0   Peter Rosin   pinctrl: sx150x: ...
798
  	{"sx1507q", (kernel_ulong_t) &sx1507q_device_data },
bba709bd7   Peter Rosin   pinctrl: sx150x: ...
799
800
  	{"sx1508q", (kernel_ulong_t) &sx1508q_device_data },
  	{"sx1509q", (kernel_ulong_t) &sx1509q_device_data },
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
801
802
803
804
  	{}
  };
  
  static const struct of_device_id sx150x_of_match[] = {
4f5ac8cf0   Peter Rosin   pinctrl: sx150x: ...
805
  	{ .compatible = "semtech,sx1501q", .data = &sx1501q_device_data },
e3ba81206   Andrey Smirnov   pinctrl-sx150x: I...
806
  	{ .compatible = "semtech,sx1502q", .data = &sx1502q_device_data },
6697546d6   Andrey Smirnov   pinctrl-sx150x: A...
807
  	{ .compatible = "semtech,sx1503q", .data = &sx1503q_device_data },
4f5ac8cf0   Peter Rosin   pinctrl: sx150x: ...
808
809
  	{ .compatible = "semtech,sx1504q", .data = &sx1504q_device_data },
  	{ .compatible = "semtech,sx1505q", .data = &sx1505q_device_data },
bba709bd7   Peter Rosin   pinctrl: sx150x: ...
810
  	{ .compatible = "semtech,sx1506q", .data = &sx1506q_device_data },
4f5ac8cf0   Peter Rosin   pinctrl: sx150x: ...
811
  	{ .compatible = "semtech,sx1507q", .data = &sx1507q_device_data },
bba709bd7   Peter Rosin   pinctrl: sx150x: ...
812
813
  	{ .compatible = "semtech,sx1508q", .data = &sx1508q_device_data },
  	{ .compatible = "semtech,sx1509q", .data = &sx1509q_device_data },
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
814
815
  	{},
  };
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
816
817
818
819
820
821
  static int sx150x_reset(struct sx150x_pinctrl *pctl)
  {
  	int err;
  
  	err = i2c_smbus_write_byte_data(pctl->client,
  					pctl->data->pri.x789.reg_reset,
f9038d603   Andrey Smirnov   pinctrl-sx150x: R...
822
  					SX150X_789_RESET_KEY1);
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
823
824
825
826
827
  	if (err < 0)
  		return err;
  
  	err = i2c_smbus_write_byte_data(pctl->client,
  					pctl->data->pri.x789.reg_reset,
f9038d603   Andrey Smirnov   pinctrl-sx150x: R...
828
  					SX150X_789_RESET_KEY2);
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
829
830
  	return err;
  }
310cdfa00   Andrey Smirnov   pinctrl-sx150x: M...
831
832
833
834
835
836
837
838
839
840
  static int sx150x_init_misc(struct sx150x_pinctrl *pctl)
  {
  	u8 reg, value;
  
  	switch (pctl->data->model) {
  	case SX150X_789:
  		reg   = pctl->data->pri.x789.reg_misc;
  		value = SX150X_789_REG_MISC_AUTOCLEAR_OFF;
  		break;
  	case SX150X_456:
c9d26f1aa   Peter Rosin   pinctrl: sx150x: ...
841
  		reg   = pctl->data->pri.x456.reg_advanced;
310cdfa00   Andrey Smirnov   pinctrl-sx150x: M...
842
  		value = 0x00;
b30d31e40   Andrey Smirnov   pinctrl-sx150x: I...
843
844
845
846
847
848
849
  
  		/*
  		 * Only SX1506 has RegAdvanced, SX1504/5 are expected
  		 * to initialize this offset to zero
  		 */
  		if (!reg)
  			return 0;
310cdfa00   Andrey Smirnov   pinctrl-sx150x: M...
850
851
  		break;
  	case SX150X_123:
c9d26f1aa   Peter Rosin   pinctrl: sx150x: ...
852
  		reg   = pctl->data->pri.x123.reg_advanced;
310cdfa00   Andrey Smirnov   pinctrl-sx150x: M...
853
854
855
856
857
858
859
  		value = 0x00;
  		break;
  	default:
  		WARN(1, "Unknown chip model %d
  ", pctl->data->model);
  		return -EINVAL;
  	}
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
860
  	return regmap_write(pctl->regmap, reg, value);
310cdfa00   Andrey Smirnov   pinctrl-sx150x: M...
861
  }
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
862
863
  static int sx150x_init_hw(struct sx150x_pinctrl *pctl)
  {
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
864
865
866
867
868
  	const u8 reg[] = {
  		[SX150X_789] = pctl->data->pri.x789.reg_polarity,
  		[SX150X_456] = pctl->data->pri.x456.reg_pld_mode,
  		[SX150X_123] = pctl->data->pri.x123.reg_pld_mode,
  	};
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
869
870
871
872
873
874
875
876
  	int err;
  
  	if (pctl->data->model == SX150X_789 &&
  	    of_property_read_bool(pctl->dev->of_node, "semtech,probe-reset")) {
  		err = sx150x_reset(pctl);
  		if (err < 0)
  			return err;
  	}
310cdfa00   Andrey Smirnov   pinctrl-sx150x: M...
877
  	err = sx150x_init_misc(pctl);
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
878
879
880
881
  	if (err < 0)
  		return err;
  
  	/* Set all pins to work in normal mode */
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
  	return regmap_write(pctl->regmap, reg[pctl->data->model], 0);
  }
  
  static int sx150x_regmap_reg_width(struct sx150x_pinctrl *pctl,
  				   unsigned int reg)
  {
  	const struct sx150x_device_data *data = pctl->data;
  
  	if (reg == data->reg_sense) {
  		/*
  		 * RegSense packs two bits of configuration per GPIO,
  		 * so we'd need to read twice as many bits as there
  		 * are GPIO in our chip
  		 */
  		return 2 * data->ngpios;
  	} else if ((data->model == SX150X_789 &&
  		    (reg == data->pri.x789.reg_misc ||
  		     reg == data->pri.x789.reg_clock ||
  		     reg == data->pri.x789.reg_reset))
  		   ||
  		   (data->model == SX150X_123 &&
c9d26f1aa   Peter Rosin   pinctrl: sx150x: ...
903
  		    reg == data->pri.x123.reg_advanced)
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
904
905
  		   ||
  		   (data->model == SX150X_456 &&
283dc0be7   Peter Rosin   pinctrl: sx150x: ...
906
  		    data->pri.x456.reg_advanced &&
c9d26f1aa   Peter Rosin   pinctrl: sx150x: ...
907
  		    reg == data->pri.x456.reg_advanced)) {
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
908
  		return 8;
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
909
  	} else {
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
  		return data->ngpios;
  	}
  }
  
  static unsigned int sx150x_maybe_swizzle(struct sx150x_pinctrl *pctl,
  					 unsigned int reg, unsigned int val)
  {
  	unsigned int a, b;
  	const struct sx150x_device_data *data = pctl->data;
  
  	/*
  	 * Whereas SX1509 presents RegSense in a simple layout as such:
  	 *	reg     [ f f e e d d c c ]
  	 *	reg + 1 [ b b a a 9 9 8 8 ]
  	 *	reg + 2 [ 7 7 6 6 5 5 4 4 ]
  	 *	reg + 3 [ 3 3 2 2 1 1 0 0 ]
  	 *
  	 * SX1503 and SX1506 deviate from that data layout, instead storing
7bd474963   Peter Rosin   pinctrl: sx150x: ...
928
  	 * their contents as follows:
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
  	 *
  	 *	reg     [ f f e e d d c c ]
  	 *	reg + 1 [ 7 7 6 6 5 5 4 4 ]
  	 *	reg + 2 [ b b a a 9 9 8 8 ]
  	 *	reg + 3 [ 3 3 2 2 1 1 0 0 ]
  	 *
  	 * so, taking that into account, we swap two
  	 * inner bytes of a 4-byte result
  	 */
  
  	if (reg == data->reg_sense &&
  	    data->ngpios == 16 &&
  	    (data->model == SX150X_123 ||
  	     data->model == SX150X_456)) {
  		a = val & 0x00ff0000;
  		b = val & 0x0000ff00;
  
  		val &= 0xff0000ff;
  		val |= b << 8;
  		val |= a >> 8;
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
949
  	}
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
950
951
952
953
954
955
  	return val;
  }
  
  /*
   * In order to mask the differences between 16 and 8 bit expander
   * devices we set up a sligthly ficticious regmap that pretends to be
d71ffeb9f   Dejin Zheng   pinctrl: fix seve...
956
   * a set of 32-bit (to accommodate RegSenseLow/RegSenseHigh
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
957
958
959
960
961
   * pair/quartet) registers and transparently reconstructs those
   * registers via multiple I2C/SMBus reads
   *
   * This way the rest of the driver code, interfacing with the chip via
   * regmap API, can work assuming that each GPIO pin is represented by
7bd474963   Peter Rosin   pinctrl: sx150x: ...
962
   * a group of bits at an offset proportional to GPIO number within a
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
963
   * given register.
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
964
965
966
967
968
969
970
971
972
973
974
   */
  static int sx150x_regmap_reg_read(void *context, unsigned int reg,
  				  unsigned int *result)
  {
  	int ret, n;
  	struct sx150x_pinctrl *pctl = context;
  	struct i2c_client *i2c = pctl->client;
  	const int width = sx150x_regmap_reg_width(pctl, reg);
  	unsigned int idx, val;
  
  	/*
7bd474963   Peter Rosin   pinctrl: sx150x: ...
975
  	 * There are four potential cases covered by this function:
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
  	 *
  	 * 1) 8-pin chip, single configuration bit register
  	 *
  	 *	This is trivial the code below just needs to read:
  	 *		reg  [ 7 6 5 4 3 2 1 0 ]
  	 *
  	 * 2) 8-pin chip, double configuration bit register (RegSense)
  	 *
  	 *	The read will be done as follows:
  	 *		reg      [ 7 7 6 6 5 5 4 4 ]
  	 *		reg + 1  [ 3 3 2 2 1 1 0 0 ]
  	 *
  	 * 3) 16-pin chip, single configuration bit register
  	 *
  	 *	The read will be done as follows:
  	 *		reg     [ f e d c b a 9 8 ]
  	 *		reg + 1 [ 7 6 5 4 3 2 1 0 ]
  	 *
  	 * 4) 16-pin chip, double configuration bit register (RegSense)
  	 *
  	 *	The read will be done as follows:
  	 *		reg     [ f f e e d d c c ]
  	 *		reg + 1 [ b b a a 9 9 8 8 ]
  	 *		reg + 2 [ 7 7 6 6 5 5 4 4 ]
  	 *		reg + 3 [ 3 3 2 2 1 1 0 0 ]
  	 */
  
  	for (n = width, val = 0, idx = reg; n > 0; n -= 8, idx++) {
  		val <<= 8;
  
  		ret = i2c_smbus_read_byte_data(i2c, idx);
  		if (ret < 0)
  			return ret;
  
  		val |= ret;
  	}
  
  	*result = sx150x_maybe_swizzle(pctl, reg, val);
  
  	return 0;
  }
  
  static int sx150x_regmap_reg_write(void *context, unsigned int reg,
  				   unsigned int val)
  {
  	int ret, n;
  	struct sx150x_pinctrl *pctl = context;
  	struct i2c_client *i2c = pctl->client;
  	const int width = sx150x_regmap_reg_width(pctl, reg);
  
  	val = sx150x_maybe_swizzle(pctl, reg, val);
6c4ef627d   Peter Rosin   pinctrl: sx150x: ...
1027
  	n = (width - 1) & ~7;
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
  	do {
  		const u8 byte = (val >> n) & 0xff;
  
  		ret = i2c_smbus_write_byte_data(i2c, reg, byte);
  		if (ret < 0)
  			return ret;
  
  		reg++;
  		n -= 8;
  	} while (n >= 0);
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
1038
1039
  	return 0;
  }
0db0f26c2   Andrey Smirnov   pinctrl-sx150x: C...
1040
1041
1042
  static bool sx150x_reg_volatile(struct device *dev, unsigned int reg)
  {
  	struct sx150x_pinctrl *pctl = i2c_get_clientdata(to_i2c_client(dev));
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
1043
  	return reg == pctl->data->reg_irq_src || reg == pctl->data->reg_data;
0db0f26c2   Andrey Smirnov   pinctrl-sx150x: C...
1044
  }
1356d86ff   Colin Ian King   pinctrl: sx150x: ...
1045
  static const struct regmap_config sx150x_regmap_config = {
0db0f26c2   Andrey Smirnov   pinctrl-sx150x: C...
1046
  	.reg_bits = 8,
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
1047
  	.val_bits = 32,
0db0f26c2   Andrey Smirnov   pinctrl-sx150x: C...
1048
1049
  
  	.cache_type = REGCACHE_RBTREE,
6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
1050
1051
  	.reg_read = sx150x_regmap_reg_read,
  	.reg_write = sx150x_regmap_reg_write,
0db0f26c2   Andrey Smirnov   pinctrl-sx150x: C...
1052
1053
1054
  	.max_register = SX150X_MAX_REGISTER,
  	.volatile_reg = sx150x_reg_volatile,
  };
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
1055
1056
1057
1058
1059
1060
1061
1062
  static int sx150x_probe(struct i2c_client *client,
  			const struct i2c_device_id *id)
  {
  	static const u32 i2c_funcs = I2C_FUNC_SMBUS_BYTE_DATA |
  				     I2C_FUNC_SMBUS_WRITE_WORD_DATA;
  	struct device *dev = &client->dev;
  	struct sx150x_pinctrl *pctl;
  	int ret;
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
1063
1064
1065
1066
1067
1068
  	if (!i2c_check_functionality(client->adapter, i2c_funcs))
  		return -ENOSYS;
  
  	pctl = devm_kzalloc(dev, sizeof(*pctl), GFP_KERNEL);
  	if (!pctl)
  		return -ENOMEM;
0db0f26c2   Andrey Smirnov   pinctrl-sx150x: C...
1069
  	i2c_set_clientdata(client, pctl);
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
1070
1071
  	pctl->dev = dev;
  	pctl->client = client;
e3ba81206   Andrey Smirnov   pinctrl-sx150x: I...
1072
1073
1074
1075
1076
1077
1078
1079
  
  	if (dev->of_node)
  		pctl->data = of_device_get_match_data(dev);
  	else
  		pctl->data = (struct sx150x_device_data *)id->driver_data;
  
  	if (!pctl->data)
  		return -EINVAL;
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
1080

6489677f8   Andrey Smirnov   pinctrl-sx150x: R...
1081
1082
  	pctl->regmap = devm_regmap_init(dev, NULL, pctl,
  					&sx150x_regmap_config);
0db0f26c2   Andrey Smirnov   pinctrl-sx150x: C...
1083
1084
1085
1086
1087
1088
1089
  	if (IS_ERR(pctl->regmap)) {
  		ret = PTR_ERR(pctl->regmap);
  		dev_err(dev, "Failed to allocate register map: %d
  ",
  			ret);
  		return ret;
  	}
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
1090
1091
1092
1093
1094
  	mutex_init(&pctl->lock);
  
  	ret = sx150x_init_hw(pctl);
  	if (ret)
  		return ret;
1a1d39e1b   Peter Rosin   pinctrl: sx150x: ...
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
  	/* Pinctrl_desc */
  	pctl->pinctrl_desc.name = "sx150x-pinctrl";
  	pctl->pinctrl_desc.pctlops = &sx150x_pinctrl_ops;
  	pctl->pinctrl_desc.confops = &sx150x_pinconf_ops;
  	pctl->pinctrl_desc.pins = pctl->data->pins;
  	pctl->pinctrl_desc.npins = pctl->data->npins;
  	pctl->pinctrl_desc.owner = THIS_MODULE;
  
  	ret = devm_pinctrl_register_and_init(dev, &pctl->pinctrl_desc,
  					     pctl, &pctl->pctldev);
  	if (ret) {
  		dev_err(dev, "Failed to register pinctrl device
  ");
  		return ret;
  	}
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
1110
  	/* Register GPIO controller */
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
1111
1112
1113
1114
1115
1116
1117
  	pctl->gpio.base = -1;
  	pctl->gpio.ngpio = pctl->data->npins;
  	pctl->gpio.get_direction = sx150x_gpio_get_direction;
  	pctl->gpio.direction_input = sx150x_gpio_direction_input;
  	pctl->gpio.direction_output = sx150x_gpio_direction_output;
  	pctl->gpio.get = sx150x_gpio_get;
  	pctl->gpio.set = sx150x_gpio_set;
2956b5d94   Mika Westerberg   pinctrl / gpio: I...
1118
  	pctl->gpio.set_config = gpiochip_generic_config;
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
1119
1120
1121
1122
1123
  	pctl->gpio.parent = dev;
  #ifdef CONFIG_OF_GPIO
  	pctl->gpio.of_node = dev->of_node;
  #endif
  	pctl->gpio.can_sleep = true;
a9d9f6b83   Nicholas Mc Guire   pinctrl: sx150x: ...
1124
1125
1126
  	pctl->gpio.label = devm_kstrdup(dev, client->name, GFP_KERNEL);
  	if (!pctl->gpio.label)
  		return -ENOMEM;
ec61168bc   Peter Rosin   pinctrl: sx150x: ...
1127
1128
1129
1130
1131
1132
1133
1134
  	/*
  	 * Setting multiple pins is not safe when all pins are not
  	 * handled by the same regmap register. The oscio pin (present
  	 * on the SX150X_789 chips) lives in its own register, so
  	 * would require locking that is not in place at this time.
  	 */
  	if (pctl->data->model != SX150X_789)
  		pctl->gpio.set_multiple = sx150x_gpio_set_multiple;
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
1135

9e80f9064   Neil Armstrong   pinctrl: Add SX15...
1136
1137
  	/* Add Interrupt support if an irq is specified */
  	if (client->irq > 0) {
0a04d767a   Linus Walleij   pinctrl: sx150x: ...
1138
  		struct gpio_irq_chip *girq;
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
1139
1140
1141
1142
1143
  		pctl->irq_chip.irq_mask = sx150x_irq_mask;
  		pctl->irq_chip.irq_unmask = sx150x_irq_unmask;
  		pctl->irq_chip.irq_set_type = sx150x_irq_set_type;
  		pctl->irq_chip.irq_bus_lock = sx150x_irq_bus_lock;
  		pctl->irq_chip.irq_bus_sync_unlock = sx150x_irq_bus_sync_unlock;
a9d9f6b83   Nicholas Mc Guire   pinctrl: sx150x: ...
1144
1145
1146
1147
  		pctl->irq_chip.name = devm_kstrdup(dev, client->name,
  						   GFP_KERNEL);
  		if (!pctl->irq_chip.name)
  			return -ENOMEM;
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
1148
1149
1150
  
  		pctl->irq.masked = ~0;
  		pctl->irq.sense = 0;
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
1151

080c489dd   Andrey Smirnov   pinctrl-sx150x: U...
1152
1153
  		/*
  		 * Because sx150x_irq_threaded_fn invokes all of the
0a04d767a   Linus Walleij   pinctrl: sx150x: ...
1154
1155
  		 * nested interrupt handlers via handle_nested_irq,
  		 * any "handler" assigned to struct gpio_irq_chip
080c489dd   Andrey Smirnov   pinctrl-sx150x: U...
1156
1157
1158
1159
1160
1161
1162
  		 * below is going to be ignored, so the choice of the
  		 * function does not matter that much.
  		 *
  		 * We set it to handle_bad_irq to avoid confusion,
  		 * plus it will be instantly noticeable if it is ever
  		 * called (should not happen)
  		 */
0a04d767a   Linus Walleij   pinctrl: sx150x: ...
1163
1164
1165
1166
1167
1168
1169
1170
1171
  		girq = &pctl->gpio.irq;
  		girq->chip = &pctl->irq_chip;
  		/* This will let us handle the parent IRQ in the driver */
  		girq->parent_handler = NULL;
  		girq->num_parents = 0;
  		girq->parents = NULL;
  		girq->default_type = IRQ_TYPE_NONE;
  		girq->handler = handle_bad_irq;
  		girq->threaded = true;
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
1172
1173
1174
1175
1176
1177
1178
1179
1180
  
  		ret = devm_request_threaded_irq(dev, client->irq, NULL,
  						sx150x_irq_thread_fn,
  						IRQF_ONESHOT | IRQF_SHARED |
  						IRQF_TRIGGER_FALLING,
  						pctl->irq_chip.name, pctl);
  		if (ret < 0)
  			return ret;
  	}
0a04d767a   Linus Walleij   pinctrl: sx150x: ...
1181
1182
1183
  	ret = devm_gpiochip_add_data(dev, &pctl->gpio, pctl);
  	if (ret)
  		return ret;
6d8e04f9d   Martin DEVERA   pinctrl: sx150x: ...
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
  	/*
  	 * Pin control functions need to be enabled AFTER registering the
  	 * GPIO chip because sx150x_pinconf_set() calls
  	 * sx150x_gpio_direction_output().
  	 */
  	ret = pinctrl_enable(pctl->pctldev);
  	if (ret) {
  		dev_err(dev, "Failed to enable pinctrl device
  ");
  		return ret;
  	}
0a04d767a   Linus Walleij   pinctrl: sx150x: ...
1195
1196
1197
1198
  	ret = gpiochip_add_pin_range(&pctl->gpio, dev_name(dev),
  				     0, 0, pctl->data->npins);
  	if (ret)
  		return ret;
9e80f9064   Neil Armstrong   pinctrl: Add SX15...
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
  	return 0;
  }
  
  static struct i2c_driver sx150x_driver = {
  	.driver = {
  		.name = "sx150x-pinctrl",
  		.of_match_table = of_match_ptr(sx150x_of_match),
  	},
  	.probe    = sx150x_probe,
  	.id_table = sx150x_id,
  };
  
  static int __init sx150x_init(void)
  {
  	return i2c_add_driver(&sx150x_driver);
  }
  subsys_initcall(sx150x_init);