mfd: vexpress: Convert custom func API to regmap
[deliverable/linux.git] / drivers / mfd / vexpress-sysreg.c
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * Copyright (C) 2012 ARM Limited
12 */
13
14 #include <linux/err.h>
15 #include <linux/gpio.h>
16 #include <linux/io.h>
17 #include <linux/leds.h>
18 #include <linux/of_address.h>
19 #include <linux/of_platform.h>
20 #include <linux/platform_device.h>
21 #include <linux/regulator/driver.h>
22 #include <linux/sched.h>
23 #include <linux/slab.h>
24 #include <linux/stat.h>
25 #include <linux/timer.h>
26 #include <linux/vexpress.h>
27
28 #define SYS_ID 0x000
29 #define SYS_SW 0x004
30 #define SYS_LED 0x008
31 #define SYS_100HZ 0x024
32 #define SYS_FLAGS 0x030
33 #define SYS_FLAGSSET 0x030
34 #define SYS_FLAGSCLR 0x034
35 #define SYS_NVFLAGS 0x038
36 #define SYS_NVFLAGSSET 0x038
37 #define SYS_NVFLAGSCLR 0x03c
38 #define SYS_MCI 0x048
39 #define SYS_FLASH 0x04c
40 #define SYS_CFGSW 0x058
41 #define SYS_24MHZ 0x05c
42 #define SYS_MISC 0x060
43 #define SYS_DMA 0x064
44 #define SYS_PROCID0 0x084
45 #define SYS_PROCID1 0x088
46 #define SYS_CFGDATA 0x0a0
47 #define SYS_CFGCTRL 0x0a4
48 #define SYS_CFGSTAT 0x0a8
49
50 #define SYS_HBI_MASK 0xfff
51 #define SYS_ID_HBI_SHIFT 16
52 #define SYS_PROCIDx_HBI_SHIFT 0
53
54 #define SYS_LED_LED(n) (1 << (n))
55
56 #define SYS_MCI_CARDIN (1 << 0)
57 #define SYS_MCI_WPROT (1 << 1)
58
59 #define SYS_FLASH_WPn (1 << 0)
60
61 #define SYS_MISC_MASTERSITE (1 << 14)
62
63 #define SYS_CFGCTRL_START (1 << 31)
64 #define SYS_CFGCTRL_WRITE (1 << 30)
65 #define SYS_CFGCTRL_DCC(n) (((n) & 0xf) << 26)
66 #define SYS_CFGCTRL_FUNC(n) (((n) & 0x3f) << 20)
67 #define SYS_CFGCTRL_SITE(n) (((n) & 0x3) << 16)
68 #define SYS_CFGCTRL_POSITION(n) (((n) & 0xf) << 12)
69 #define SYS_CFGCTRL_DEVICE(n) (((n) & 0xfff) << 0)
70
71 #define SYS_CFGSTAT_ERR (1 << 1)
72 #define SYS_CFGSTAT_COMPLETE (1 << 0)
73
74
75 static void __iomem *vexpress_sysreg_base;
76 static struct device *vexpress_sysreg_dev;
77 static LIST_HEAD(vexpress_sysreg_config_funcs);
78 static struct device *vexpress_sysreg_config_bridge;
79
80
81 static int vexpress_sysreg_get_master(void)
82 {
83 if (readl(vexpress_sysreg_base + SYS_MISC) & SYS_MISC_MASTERSITE)
84 return VEXPRESS_SITE_DB2;
85
86 return VEXPRESS_SITE_DB1;
87 }
88
89 void vexpress_flags_set(u32 data)
90 {
91 writel(~0, vexpress_sysreg_base + SYS_FLAGSCLR);
92 writel(data, vexpress_sysreg_base + SYS_FLAGSSET);
93 }
94
95 u32 vexpress_get_procid(int site)
96 {
97 if (site == VEXPRESS_SITE_MASTER)
98 site = vexpress_sysreg_get_master();
99
100 return readl(vexpress_sysreg_base + (site == VEXPRESS_SITE_DB1 ?
101 SYS_PROCID0 : SYS_PROCID1));
102 }
103
104 u32 vexpress_get_hbi(int site)
105 {
106 u32 id;
107
108 switch (site) {
109 case VEXPRESS_SITE_MB:
110 id = readl(vexpress_sysreg_base + SYS_ID);
111 return (id >> SYS_ID_HBI_SHIFT) & SYS_HBI_MASK;
112 case VEXPRESS_SITE_MASTER:
113 case VEXPRESS_SITE_DB1:
114 case VEXPRESS_SITE_DB2:
115 id = vexpress_get_procid(site);
116 return (id >> SYS_PROCIDx_HBI_SHIFT) & SYS_HBI_MASK;
117 }
118
119 return ~0;
120 }
121
122 void __iomem *vexpress_get_24mhz_clock_base(void)
123 {
124 return vexpress_sysreg_base + SYS_24MHZ;
125 }
126
127
128 struct vexpress_sysreg_config_func {
129 struct list_head list;
130 struct regmap *regmap;
131 int num_templates;
132 u32 template[0]; /* Keep this last */
133 };
134
135 static int vexpress_sysreg_config_exec(struct vexpress_sysreg_config_func *func,
136 int index, bool write, u32 *data)
137 {
138 u32 command, status;
139 int tries;
140 long timeout;
141
142 if (WARN_ON(!vexpress_sysreg_base))
143 return -ENOENT;
144
145 if (WARN_ON(index > func->num_templates))
146 return -EINVAL;
147
148 command = readl(vexpress_sysreg_base + SYS_CFGCTRL);
149 if (WARN_ON(command & SYS_CFGCTRL_START))
150 return -EBUSY;
151
152 command = func->template[index];
153 command |= SYS_CFGCTRL_START;
154 command |= write ? SYS_CFGCTRL_WRITE : 0;
155
156 /* Use a canary for reads */
157 if (!write)
158 *data = 0xdeadbeef;
159
160 dev_dbg(vexpress_sysreg_dev, "command %x, data %x\n",
161 command, *data);
162 writel(*data, vexpress_sysreg_base + SYS_CFGDATA);
163 writel(0, vexpress_sysreg_base + SYS_CFGSTAT);
164 writel(command, vexpress_sysreg_base + SYS_CFGCTRL);
165 mb();
166
167 /* The operation can take ages... Go to sleep, 100us initially */
168 tries = 100;
169 timeout = 100;
170 do {
171 set_current_state(TASK_INTERRUPTIBLE);
172 schedule_timeout(usecs_to_jiffies(timeout));
173 if (signal_pending(current))
174 return -EINTR;
175
176 status = readl(vexpress_sysreg_base + SYS_CFGSTAT);
177 if (status & SYS_CFGSTAT_ERR)
178 return -EFAULT;
179
180 if (timeout > 20)
181 timeout -= 20;
182 } while (--tries && !(status & SYS_CFGSTAT_COMPLETE));
183 if (WARN_ON_ONCE(!tries))
184 return -ETIMEDOUT;
185
186 if (!write) {
187 *data = readl(vexpress_sysreg_base + SYS_CFGDATA);
188 dev_dbg(vexpress_sysreg_dev, "func %p, read data %x\n",
189 func, *data);
190 }
191
192 return 0;
193 }
194
195 static int vexpress_sysreg_config_read(void *context, unsigned int index,
196 unsigned int *val)
197 {
198 struct vexpress_sysreg_config_func *func = context;
199
200 return vexpress_sysreg_config_exec(func, index, false, val);
201 }
202
203 static int vexpress_sysreg_config_write(void *context, unsigned int index,
204 unsigned int val)
205 {
206 struct vexpress_sysreg_config_func *func = context;
207
208 return vexpress_sysreg_config_exec(func, index, true, &val);
209 }
210
211 struct regmap_config vexpress_sysreg_regmap_config = {
212 .lock = vexpress_config_lock,
213 .unlock = vexpress_config_unlock,
214 .reg_bits = 32,
215 .val_bits = 32,
216 .reg_read = vexpress_sysreg_config_read,
217 .reg_write = vexpress_sysreg_config_write,
218 .reg_format_endian = REGMAP_ENDIAN_LITTLE,
219 .val_format_endian = REGMAP_ENDIAN_LITTLE,
220 };
221
222 static struct regmap *vexpress_sysreg_config_regmap_init(struct device *dev,
223 void *context)
224 {
225 struct platform_device *pdev = to_platform_device(dev);
226 struct vexpress_sysreg_config_func *func;
227 struct property *prop;
228 const __be32 *val = NULL;
229 __be32 energy_quirk[4];
230 int num;
231 u32 site, position, dcc;
232 int err;
233 int i;
234
235 if (dev->of_node) {
236 err = vexpress_config_get_topo(dev->of_node, &site, &position,
237 &dcc);
238 if (err)
239 return ERR_PTR(err);
240
241 prop = of_find_property(dev->of_node,
242 "arm,vexpress-sysreg,func", NULL);
243 if (!prop)
244 return ERR_PTR(-EINVAL);
245
246 num = prop->length / sizeof(u32) / 2;
247 val = prop->value;
248 } else {
249 if (pdev->num_resources != 1 ||
250 pdev->resource[0].flags != IORESOURCE_BUS)
251 return ERR_PTR(-EFAULT);
252
253 site = pdev->resource[0].start;
254 if (site == VEXPRESS_SITE_MASTER)
255 site = vexpress_sysreg_get_master();
256 position = 0;
257 dcc = 0;
258 num = 1;
259 }
260
261 /*
262 * "arm,vexpress-energy" function used to be described
263 * by its first device only, now it requires both
264 */
265 if (num == 1 && of_device_is_compatible(dev->of_node,
266 "arm,vexpress-energy")) {
267 num = 2;
268 energy_quirk[0] = *val;
269 energy_quirk[2] = *val++;
270 energy_quirk[1] = *val;
271 energy_quirk[3] = cpu_to_be32(be32_to_cpup(val) + 1);
272 val = energy_quirk;
273 }
274
275 func = kzalloc(sizeof(*func) + sizeof(*func->template) * num,
276 GFP_KERNEL);
277 if (!func)
278 return NULL;
279
280 func->num_templates = num;
281
282 for (i = 0; i < num; i++) {
283 u32 function, device;
284
285 if (dev->of_node) {
286 function = be32_to_cpup(val++);
287 device = be32_to_cpup(val++);
288 } else {
289 function = pdev->resource[0].end;
290 device = pdev->id;
291 }
292
293 dev_dbg(dev, "func %p: %u/%u/%u/%u/%u\n",
294 func, site, position, dcc,
295 function, device);
296
297 func->template[i] = SYS_CFGCTRL_DCC(dcc);
298 func->template[i] |= SYS_CFGCTRL_SITE(site);
299 func->template[i] |= SYS_CFGCTRL_POSITION(position);
300 func->template[i] |= SYS_CFGCTRL_FUNC(function);
301 func->template[i] |= SYS_CFGCTRL_DEVICE(device);
302 }
303
304 vexpress_sysreg_regmap_config.max_register = num - 1;
305
306 func->regmap = regmap_init(dev, NULL, func,
307 &vexpress_sysreg_regmap_config);
308
309 if (IS_ERR(func->regmap))
310 kfree(func);
311 else
312 list_add(&func->list, &vexpress_sysreg_config_funcs);
313
314 return func->regmap;
315 }
316
317 static void vexpress_sysreg_config_regmap_exit(struct regmap *regmap,
318 void *context)
319 {
320 struct vexpress_sysreg_config_func *func, *tmp;
321
322 regmap_exit(regmap);
323
324 list_for_each_entry_safe(func, tmp, &vexpress_sysreg_config_funcs,
325 list) {
326 if (func->regmap == regmap) {
327 list_del(&vexpress_sysreg_config_funcs);
328 kfree(func);
329 break;
330 }
331 }
332 }
333
334 static struct vexpress_config_bridge_ops vexpress_sysreg_config_bridge_ops = {
335 .regmap_init = vexpress_sysreg_config_regmap_init,
336 .regmap_exit = vexpress_sysreg_config_regmap_exit,
337 };
338
339 int vexpress_sysreg_config_device_register(struct platform_device *pdev)
340 {
341 pdev->dev.parent = vexpress_sysreg_config_bridge;
342
343 return platform_device_register(pdev);
344 }
345
346
347 void __init vexpress_sysreg_early_init(void __iomem *base)
348 {
349 vexpress_sysreg_base = base;
350 vexpress_config_set_master(vexpress_sysreg_get_master());
351 }
352
353 void __init vexpress_sysreg_of_early_init(void)
354 {
355 struct device_node *node;
356
357 if (vexpress_sysreg_base)
358 return;
359
360 node = of_find_compatible_node(NULL, NULL, "arm,vexpress-sysreg");
361 if (WARN_ON(!node))
362 return;
363
364 vexpress_sysreg_base = of_iomap(node, 0);
365 if (WARN_ON(!vexpress_sysreg_base))
366 return;
367
368 vexpress_config_set_master(vexpress_sysreg_get_master());
369 }
370
371
372 #ifdef CONFIG_GPIOLIB
373
374 #define VEXPRESS_SYSREG_GPIO(_name, _reg, _value) \
375 [VEXPRESS_GPIO_##_name] = { \
376 .reg = _reg, \
377 .value = _reg##_##_value, \
378 }
379
380 static struct vexpress_sysreg_gpio {
381 unsigned long reg;
382 u32 value;
383 } vexpress_sysreg_gpios[] = {
384 VEXPRESS_SYSREG_GPIO(MMC_CARDIN, SYS_MCI, CARDIN),
385 VEXPRESS_SYSREG_GPIO(MMC_WPROT, SYS_MCI, WPROT),
386 VEXPRESS_SYSREG_GPIO(FLASH_WPn, SYS_FLASH, WPn),
387 VEXPRESS_SYSREG_GPIO(LED0, SYS_LED, LED(0)),
388 VEXPRESS_SYSREG_GPIO(LED1, SYS_LED, LED(1)),
389 VEXPRESS_SYSREG_GPIO(LED2, SYS_LED, LED(2)),
390 VEXPRESS_SYSREG_GPIO(LED3, SYS_LED, LED(3)),
391 VEXPRESS_SYSREG_GPIO(LED4, SYS_LED, LED(4)),
392 VEXPRESS_SYSREG_GPIO(LED5, SYS_LED, LED(5)),
393 VEXPRESS_SYSREG_GPIO(LED6, SYS_LED, LED(6)),
394 VEXPRESS_SYSREG_GPIO(LED7, SYS_LED, LED(7)),
395 };
396
397 static int vexpress_sysreg_gpio_direction_input(struct gpio_chip *chip,
398 unsigned offset)
399 {
400 return 0;
401 }
402
403 static int vexpress_sysreg_gpio_get(struct gpio_chip *chip,
404 unsigned offset)
405 {
406 struct vexpress_sysreg_gpio *gpio = &vexpress_sysreg_gpios[offset];
407 u32 reg_value = readl(vexpress_sysreg_base + gpio->reg);
408
409 return !!(reg_value & gpio->value);
410 }
411
412 static void vexpress_sysreg_gpio_set(struct gpio_chip *chip,
413 unsigned offset, int value)
414 {
415 struct vexpress_sysreg_gpio *gpio = &vexpress_sysreg_gpios[offset];
416 u32 reg_value = readl(vexpress_sysreg_base + gpio->reg);
417
418 if (value)
419 reg_value |= gpio->value;
420 else
421 reg_value &= ~gpio->value;
422
423 writel(reg_value, vexpress_sysreg_base + gpio->reg);
424 }
425
426 static int vexpress_sysreg_gpio_direction_output(struct gpio_chip *chip,
427 unsigned offset, int value)
428 {
429 vexpress_sysreg_gpio_set(chip, offset, value);
430
431 return 0;
432 }
433
434 static struct gpio_chip vexpress_sysreg_gpio_chip = {
435 .label = "vexpress-sysreg",
436 .direction_input = vexpress_sysreg_gpio_direction_input,
437 .direction_output = vexpress_sysreg_gpio_direction_output,
438 .get = vexpress_sysreg_gpio_get,
439 .set = vexpress_sysreg_gpio_set,
440 .ngpio = ARRAY_SIZE(vexpress_sysreg_gpios),
441 .base = 0,
442 };
443
444
445 #define VEXPRESS_SYSREG_GREEN_LED(_name, _default_trigger, _gpio) \
446 { \
447 .name = "v2m:green:"_name, \
448 .default_trigger = _default_trigger, \
449 .gpio = VEXPRESS_GPIO_##_gpio, \
450 }
451
452 struct gpio_led vexpress_sysreg_leds[] = {
453 VEXPRESS_SYSREG_GREEN_LED("user1", "heartbeat", LED0),
454 VEXPRESS_SYSREG_GREEN_LED("user2", "mmc0", LED1),
455 VEXPRESS_SYSREG_GREEN_LED("user3", "cpu0", LED2),
456 VEXPRESS_SYSREG_GREEN_LED("user4", "cpu1", LED3),
457 VEXPRESS_SYSREG_GREEN_LED("user5", "cpu2", LED4),
458 VEXPRESS_SYSREG_GREEN_LED("user6", "cpu3", LED5),
459 VEXPRESS_SYSREG_GREEN_LED("user7", "cpu4", LED6),
460 VEXPRESS_SYSREG_GREEN_LED("user8", "cpu5", LED7),
461 };
462
463 struct gpio_led_platform_data vexpress_sysreg_leds_pdata = {
464 .num_leds = ARRAY_SIZE(vexpress_sysreg_leds),
465 .leds = vexpress_sysreg_leds,
466 };
467
468 #endif
469
470
471 static ssize_t vexpress_sysreg_sys_id_show(struct device *dev,
472 struct device_attribute *attr, char *buf)
473 {
474 return sprintf(buf, "0x%08x\n", readl(vexpress_sysreg_base + SYS_ID));
475 }
476
477 DEVICE_ATTR(sys_id, S_IRUGO, vexpress_sysreg_sys_id_show, NULL);
478
479 static int vexpress_sysreg_probe(struct platform_device *pdev)
480 {
481 int err;
482 struct resource *res = platform_get_resource(pdev,
483 IORESOURCE_MEM, 0);
484
485 if (!devm_request_mem_region(&pdev->dev, res->start,
486 resource_size(res), pdev->name)) {
487 dev_err(&pdev->dev, "Failed to request memory region!\n");
488 return -EBUSY;
489 }
490
491 if (!vexpress_sysreg_base)
492 vexpress_sysreg_base = devm_ioremap(&pdev->dev, res->start,
493 resource_size(res));
494
495 if (!vexpress_sysreg_base) {
496 dev_err(&pdev->dev, "Failed to obtain base address!\n");
497 return -EFAULT;
498 }
499
500 vexpress_config_set_master(vexpress_sysreg_get_master());
501 vexpress_sysreg_dev = &pdev->dev;
502
503 #ifdef CONFIG_GPIOLIB
504 vexpress_sysreg_gpio_chip.dev = &pdev->dev;
505 err = gpiochip_add(&vexpress_sysreg_gpio_chip);
506 if (err) {
507 dev_err(&pdev->dev, "Failed to register GPIO chip! (%d)\n",
508 err);
509 return err;
510 }
511
512 platform_device_register_data(vexpress_sysreg_dev, "leds-gpio",
513 PLATFORM_DEVID_AUTO, &vexpress_sysreg_leds_pdata,
514 sizeof(vexpress_sysreg_leds_pdata));
515 #endif
516
517 vexpress_sysreg_config_bridge = vexpress_config_bridge_register(
518 &pdev->dev, &vexpress_sysreg_config_bridge_ops, NULL);
519 WARN_ON(!vexpress_sysreg_config_bridge);
520
521 device_create_file(vexpress_sysreg_dev, &dev_attr_sys_id);
522
523 return 0;
524 }
525
526 static const struct of_device_id vexpress_sysreg_match[] = {
527 { .compatible = "arm,vexpress-sysreg", },
528 {},
529 };
530
531 static struct platform_driver vexpress_sysreg_driver = {
532 .driver = {
533 .name = "vexpress-sysreg",
534 .of_match_table = vexpress_sysreg_match,
535 },
536 .probe = vexpress_sysreg_probe,
537 };
538
539 static int __init vexpress_sysreg_init(void)
540 {
541 struct device_node *node;
542
543 /* Need the sysreg early, before any other device... */
544 for_each_matching_node(node, vexpress_sysreg_match)
545 of_platform_device_create(node, NULL, NULL);
546
547 return platform_driver_register(&vexpress_sysreg_driver);
548 }
549 core_initcall(vexpress_sysreg_init);
This page took 0.041174 seconds and 5 git commands to generate.