ARM64: DTS: Add rk3399-firefly uart4 device, node as /dev/ttyS1
[firefly-linux-kernel-4.4.55.git] / drivers / power / avs / rockchip-io-domain.c
1 /*
2  * Rockchip IO Voltage Domain driver
3  *
4  * Copyright 2014 MundoReader S.L.
5  * Copyright 2014 Google, Inc.
6  *
7  * This software is licensed under the terms of the GNU General Public
8  * License version 2, as published by the Free Software Foundation, and
9  * may be copied, distributed, and modified under those terms.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  */
16
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/err.h>
20 #include <linux/mfd/syscon.h>
21 #include <linux/of.h>
22 #include <linux/platform_device.h>
23 #include <linux/regmap.h>
24 #include <linux/regulator/consumer.h>
25
26 #define MAX_SUPPLIES            16
27
28 /*
29  * The max voltage for 1.8V and 3.3V come from the Rockchip datasheet under
30  * "Recommended Operating Conditions" for "Digital GPIO".   When the typical
31  * is 3.3V the max is 3.6V.  When the typical is 1.8V the max is 1.98V.
32  *
33  * They are used like this:
34  * - If the voltage on a rail is above the "1.8" voltage (1.98V) we'll tell the
35  *   SoC we're at 3.3.
36  * - If the voltage on a rail is above the "3.3" voltage (3.6V) we'll consider
37  *   that to be an error.
38  */
39 #define MAX_VOLTAGE_1_8         1980000
40 #define MAX_VOLTAGE_3_3         3600000
41
42 #define RK3288_SOC_CON2                 0x24c
43 #define RK3288_SOC_CON2_FLASH0          BIT(7)
44 #define RK3288_SOC_FLASH_SUPPLY_NUM     2
45
46 #define RK3328_SOC_CON4                 0x410
47 #define RK3328_SOC_CON4_VCCIO2          BIT(7)
48 #define RK3328_SOC_VCCIO2_SUPPLY_NUM    1
49
50 #define RK3366_SOC_CON6                 0x418
51 #define RK3366_SOC_CON6_FLASH0          BIT(14)
52 #define RK3366_SOC_FLASH_SUPPLY_NUM     2
53
54 #define RK3368_SOC_CON15                0x43c
55 #define RK3368_SOC_CON15_FLASH0         BIT(14)
56 #define RK3368_SOC_FLASH_SUPPLY_NUM     2
57
58 #define RK3399_PMUGRF_CON0              0x180
59 #define RK3399_PMUGRF_CON0_VSEL         BIT(8)
60 #define RK3399_PMUGRF_VSEL_SUPPLY_NUM   9
61
62 struct rockchip_iodomain;
63
64 /**
65  * @supplies: voltage settings matching the register bits.
66  */
67 struct rockchip_iodomain_soc_data {
68         int grf_offset;
69         const char *supply_names[MAX_SUPPLIES];
70         void (*init)(struct rockchip_iodomain *iod);
71 };
72
73 struct rockchip_iodomain_supply {
74         struct rockchip_iodomain *iod;
75         struct regulator *reg;
76         struct notifier_block nb;
77         int idx;
78 };
79
80 struct rockchip_iodomain {
81         struct device *dev;
82         struct regmap *grf;
83         struct rockchip_iodomain_soc_data *soc_data;
84         struct rockchip_iodomain_supply supplies[MAX_SUPPLIES];
85 };
86
87 static int rockchip_iodomain_write(struct rockchip_iodomain_supply *supply,
88                                    int uV)
89 {
90         struct rockchip_iodomain *iod = supply->iod;
91         u32 val;
92         int ret;
93
94         /* set value bit */
95         val = (uV > MAX_VOLTAGE_1_8) ? 0 : 1;
96         val <<= supply->idx;
97
98         /* apply hiword-mask */
99         val |= (BIT(supply->idx) << 16);
100
101         ret = regmap_write(iod->grf, iod->soc_data->grf_offset, val);
102         if (ret)
103                 dev_err(iod->dev, "Couldn't write to GRF\n");
104
105         return ret;
106 }
107
108 static int rockchip_iodomain_notify(struct notifier_block *nb,
109                                     unsigned long event,
110                                     void *data)
111 {
112         struct rockchip_iodomain_supply *supply =
113                         container_of(nb, struct rockchip_iodomain_supply, nb);
114         int uV;
115         int ret;
116
117         /*
118          * According to Rockchip it's important to keep the SoC IO domain
119          * higher than (or equal to) the external voltage.  That means we need
120          * to change it before external voltage changes happen in the case
121          * of an increase.
122          *
123          * Note that in the "pre" change we pick the max possible voltage that
124          * the regulator might end up at (the client requests a range and we
125          * don't know for certain the exact voltage).  Right now we rely on the
126          * slop in MAX_VOLTAGE_1_8 and MAX_VOLTAGE_3_3 to save us if clients
127          * request something like a max of 3.6V when they really want 3.3V.
128          * We could attempt to come up with better rules if this fails.
129          */
130         if (event & REGULATOR_EVENT_PRE_VOLTAGE_CHANGE) {
131                 struct pre_voltage_change_data *pvc_data = data;
132
133                 uV = max_t(unsigned long, pvc_data->old_uV, pvc_data->max_uV);
134         } else if (event & (REGULATOR_EVENT_VOLTAGE_CHANGE |
135                             REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE)) {
136                 uV = (unsigned long)data;
137         } else {
138                 return NOTIFY_OK;
139         }
140
141         dev_dbg(supply->iod->dev, "Setting to %d\n", uV);
142
143         if (uV > MAX_VOLTAGE_3_3) {
144                 dev_err(supply->iod->dev, "Voltage too high: %d\n", uV);
145
146                 if (event == REGULATOR_EVENT_PRE_VOLTAGE_CHANGE)
147                         return NOTIFY_BAD;
148         }
149
150         ret = rockchip_iodomain_write(supply, uV);
151         if (ret && event == REGULATOR_EVENT_PRE_VOLTAGE_CHANGE)
152                 return NOTIFY_BAD;
153
154         dev_info(supply->iod->dev, "Setting to %d done\n", uV);
155         return NOTIFY_OK;
156 }
157
158 static void rk3288_iodomain_init(struct rockchip_iodomain *iod)
159 {
160         int ret;
161         u32 val;
162
163         /* if no flash supply we should leave things alone */
164         if (!iod->supplies[RK3288_SOC_FLASH_SUPPLY_NUM].reg)
165                 return;
166
167         /*
168          * set flash0 iodomain to also use this framework
169          * instead of a special gpio.
170          */
171         val = RK3288_SOC_CON2_FLASH0 | (RK3288_SOC_CON2_FLASH0 << 16);
172         ret = regmap_write(iod->grf, RK3288_SOC_CON2, val);
173         if (ret < 0)
174                 dev_warn(iod->dev, "couldn't update flash0 ctrl\n");
175 }
176
177 static void rk3328_iodomain_init(struct rockchip_iodomain *iod)
178 {
179         int ret;
180         u32 val;
181
182         /* if no vccio2 supply we should leave things alone */
183         if (!iod->supplies[RK3328_SOC_VCCIO2_SUPPLY_NUM].reg)
184                 return;
185
186         /*
187          * set vccio2 iodomain to also use this framework
188          * instead of a special gpio.
189          */
190         val = RK3328_SOC_CON4_VCCIO2 | (RK3328_SOC_CON4_VCCIO2 << 16);
191         ret = regmap_write(iod->grf, RK3328_SOC_CON4, val);
192         if (ret < 0)
193                 dev_warn(iod->dev, "couldn't update vccio2 vsel ctrl\n");
194 }
195
196 static void rk3366_iodomain_init(struct rockchip_iodomain *iod)
197 {
198         int ret;
199         u32 val;
200
201         /* if no flash supply we should leave things alone */
202         if (!iod->supplies[RK3366_SOC_FLASH_SUPPLY_NUM].reg)
203                 return;
204
205         /*
206          * set flash0 iodomain to also use this framework
207          * instead of a special gpio.
208          */
209         val = RK3366_SOC_CON6_FLASH0 | (RK3366_SOC_CON6_FLASH0 << 16);
210         ret = regmap_write(iod->grf, RK3368_SOC_CON15, val);
211         if (ret < 0)
212                 dev_warn(iod->dev, "couldn't update flash0 ctrl\n");
213 }
214
215 static void rk3368_iodomain_init(struct rockchip_iodomain *iod)
216 {
217         int ret;
218         u32 val;
219
220         /* if no flash supply we should leave things alone */
221         if (!iod->supplies[RK3368_SOC_FLASH_SUPPLY_NUM].reg)
222                 return;
223
224         /*
225          * set flash0 iodomain to also use this framework
226          * instead of a special gpio.
227          */
228         val = RK3368_SOC_CON15_FLASH0 | (RK3368_SOC_CON15_FLASH0 << 16);
229         ret = regmap_write(iod->grf, RK3368_SOC_CON15, val);
230         if (ret < 0)
231                 dev_warn(iod->dev, "couldn't update flash0 ctrl\n");
232 }
233
234 static void rk3399_pmu_iodomain_init(struct rockchip_iodomain *iod)
235 {
236         int ret;
237         u32 val;
238
239         /* if no pmu io supply we should leave things alone */
240         if (!iod->supplies[RK3399_PMUGRF_VSEL_SUPPLY_NUM].reg)
241                 return;
242
243         /*
244          * set pmu io iodomain to also use this framework
245          * instead of a special gpio.
246          */
247         val = RK3399_PMUGRF_CON0_VSEL | (RK3399_PMUGRF_CON0_VSEL << 16);
248         ret = regmap_write(iod->grf, RK3399_PMUGRF_CON0, val);
249         if (ret < 0)
250                 dev_warn(iod->dev, "couldn't update pmu io iodomain ctrl\n");
251 }
252
253 /*
254  * On the rk3188 the io-domains are handled by a shared register with the
255  * lower 8 bits being still being continuing drive-strength settings.
256  */
257 static const struct rockchip_iodomain_soc_data soc_data_rk3188 = {
258         .grf_offset = 0x104,
259         .supply_names = {
260                 NULL,
261                 NULL,
262                 NULL,
263                 NULL,
264                 NULL,
265                 NULL,
266                 NULL,
267                 NULL,
268                 "ap0",
269                 "ap1",
270                 "cif",
271                 "flash",
272                 "vccio0",
273                 "vccio1",
274                 "lcdc0",
275                 "lcdc1",
276         },
277 };
278
279 static const struct rockchip_iodomain_soc_data soc_data_rk3288 = {
280         .grf_offset = 0x380,
281         .supply_names = {
282                 "lcdc",         /* LCDC_VDD */
283                 "dvp",          /* DVPIO_VDD */
284                 "flash0",       /* FLASH0_VDD (emmc) */
285                 "flash1",       /* FLASH1_VDD (sdio1) */
286                 "wifi",         /* APIO3_VDD  (sdio0) */
287                 "bb",           /* APIO5_VDD */
288                 "audio",        /* APIO4_VDD */
289                 "sdcard",       /* SDMMC0_VDD (sdmmc) */
290                 "gpio30",       /* APIO1_VDD */
291                 "gpio1830",     /* APIO2_VDD */
292         },
293         .init = rk3288_iodomain_init,
294 };
295
296 static const struct rockchip_iodomain_soc_data soc_data_rk3328 = {
297         .grf_offset = 0x410,
298         .supply_names = {
299                 "vccio1",
300                 "vccio2",
301                 "vccio3",
302                 "vccio4",
303                 "vccio5",
304                 "vccio6",
305                 "pmuio",
306         },
307         .init = rk3328_iodomain_init,
308 };
309
310 static const struct rockchip_iodomain_soc_data soc_data_rk3366 = {
311         .grf_offset = 0x900,
312         .supply_names = {
313                 "lcdc",         /* LCDC_IOVDD */
314                 "dvpts",        /* DVP_IOVDD */
315                 "flash",        /* FLASH_IOVDD (emmc) */
316                 "wifibt",       /* APIO1_IOVDD */
317                 NULL,
318                 "audio",        /* AUDIO_IODVDD */
319                 "sdcard",       /* SDMMC_IOVDD (sdmmc) */
320                 "tphdsor",      /* APIO2_IOVDD */
321         },
322         .init = rk3366_iodomain_init,
323 };
324
325 static const struct rockchip_iodomain_soc_data soc_data_rk3368 = {
326         .grf_offset = 0x900,
327         .supply_names = {
328                 NULL,           /* reserved */
329                 "dvp",          /* DVPIO_VDD */
330                 "flash0",       /* FLASH0_VDD (emmc) */
331                 "wifi",         /* APIO2_VDD (sdio0) */
332                 NULL,
333                 "audio",        /* APIO3_VDD */
334                 "sdcard",       /* SDMMC0_VDD (sdmmc) */
335                 "gpio30",       /* APIO1_VDD */
336                 "gpio1830",     /* APIO4_VDD (gpujtag) */
337         },
338         .init = rk3368_iodomain_init,
339 };
340
341 static const struct rockchip_iodomain_soc_data soc_data_rk3368_pmu = {
342         .grf_offset = 0x100,
343         .supply_names = {
344                 NULL,
345                 NULL,
346                 NULL,
347                 NULL,
348                 "pmu",          /*PMU IO domain*/
349                 "vop",          /*LCDC IO domain*/
350         },
351 };
352
353 static const struct rockchip_iodomain_soc_data soc_data_rk3399 = {
354         .grf_offset = 0xe640,
355         .supply_names = {
356                 "bt656",                /* APIO2_VDD */
357                 "audio",                /* APIO5_VDD */
358                 "sdmmc",                /* SDMMC0_VDD */
359                 "gpio1830",             /* APIO4_VDD */
360         },
361 };
362
363 static const struct rockchip_iodomain_soc_data soc_data_rk3399_pmu = {
364         .grf_offset = 0x180,
365         .supply_names = {
366                 NULL,
367                 NULL,
368                 NULL,
369                 NULL,
370                 NULL,
371                 NULL,
372                 NULL,
373                 NULL,
374                 NULL,
375                 "pmu1830",              /* PMUIO2_VDD */
376         },
377         .init = rk3399_pmu_iodomain_init,
378 };
379
380 static const struct of_device_id rockchip_iodomain_match[] = {
381         {
382                 .compatible = "rockchip,rk3188-io-voltage-domain",
383                 .data = (void *)&soc_data_rk3188
384         },
385         {
386                 .compatible = "rockchip,rk3288-io-voltage-domain",
387                 .data = (void *)&soc_data_rk3288
388         },
389         {
390                 .compatible = "rockchip,rk3328-io-voltage-domain",
391                 .data = (void *)&soc_data_rk3328
392         },
393         {
394                 .compatible = "rockchip,rk3366-io-voltage-domain",
395                 .data = (void *)&soc_data_rk3366
396         },
397         {
398                 .compatible = "rockchip,rk3368-io-voltage-domain",
399                 .data = (void *)&soc_data_rk3368
400         },
401         {
402                 .compatible = "rockchip,rk3368-pmu-io-voltage-domain",
403                 .data = (void *)&soc_data_rk3368_pmu
404         },
405         {
406                 .compatible = "rockchip,rk3399-io-voltage-domain",
407                 .data = (void *)&soc_data_rk3399
408         },
409         {
410                 .compatible = "rockchip,rk3399-pmu-io-voltage-domain",
411                 .data = (void *)&soc_data_rk3399_pmu
412         },
413         { /* sentinel */ },
414 };
415 MODULE_DEVICE_TABLE(of, rockchip_iodomain_match);
416
417 static int rockchip_iodomain_probe(struct platform_device *pdev)
418 {
419         struct device_node *np = pdev->dev.of_node;
420         const struct of_device_id *match;
421         struct rockchip_iodomain *iod;
422         struct device *parent;
423         int i, ret = 0;
424
425         if (!np)
426                 return -ENODEV;
427
428         iod = devm_kzalloc(&pdev->dev, sizeof(*iod), GFP_KERNEL);
429         if (!iod)
430                 return -ENOMEM;
431
432         iod->dev = &pdev->dev;
433         platform_set_drvdata(pdev, iod);
434
435         match = of_match_node(rockchip_iodomain_match, np);
436         iod->soc_data = (struct rockchip_iodomain_soc_data *)match->data;
437
438         parent = pdev->dev.parent;
439         if (parent && parent->of_node) {
440                 iod->grf = syscon_node_to_regmap(parent->of_node);
441         } else {
442                 dev_dbg(&pdev->dev, "falling back to old binding\n");
443                 iod->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
444         }
445
446         if (IS_ERR(iod->grf)) {
447                 dev_err(&pdev->dev, "couldn't find grf regmap\n");
448                 return PTR_ERR(iod->grf);
449         }
450
451         for (i = 0; i < MAX_SUPPLIES; i++) {
452                 const char *supply_name = iod->soc_data->supply_names[i];
453                 struct rockchip_iodomain_supply *supply = &iod->supplies[i];
454                 struct regulator *reg;
455                 int uV;
456
457                 if (!supply_name)
458                         continue;
459
460                 reg = devm_regulator_get_optional(iod->dev, supply_name);
461                 if (IS_ERR(reg)) {
462                         ret = PTR_ERR(reg);
463
464                         /* If a supply wasn't specified, that's OK */
465                         if (ret == -ENODEV)
466                                 continue;
467                         else if (ret != -EPROBE_DEFER)
468                                 dev_err(iod->dev, "couldn't get regulator %s\n",
469                                         supply_name);
470                         goto unreg_notify;
471                 }
472
473                 /* set initial correct value */
474                 uV = regulator_get_voltage(reg);
475
476                 /* must be a regulator we can get the voltage of */
477                 if (uV < 0) {
478                         dev_err(iod->dev, "Can't determine voltage: %s\n",
479                                 supply_name);
480                         goto unreg_notify;
481                 }
482
483                 if (uV > MAX_VOLTAGE_3_3) {
484                         dev_crit(iod->dev,
485                                  "%d uV is too high. May damage SoC!\n",
486                                  uV);
487                         ret = -EINVAL;
488                         goto unreg_notify;
489                 }
490
491                 /* setup our supply */
492                 supply->idx = i;
493                 supply->iod = iod;
494                 supply->reg = reg;
495                 supply->nb.notifier_call = rockchip_iodomain_notify;
496
497                 ret = rockchip_iodomain_write(supply, uV);
498                 if (ret) {
499                         supply->reg = NULL;
500                         goto unreg_notify;
501                 }
502
503                 /* register regulator notifier */
504                 ret = regulator_register_notifier(reg, &supply->nb);
505                 if (ret) {
506                         dev_err(&pdev->dev,
507                                 "regulator notifier request failed\n");
508                         supply->reg = NULL;
509                         goto unreg_notify;
510                 }
511         }
512
513         if (iod->soc_data->init)
514                 iod->soc_data->init(iod);
515
516         return 0;
517
518 unreg_notify:
519         for (i = MAX_SUPPLIES - 1; i >= 0; i--) {
520                 struct rockchip_iodomain_supply *io_supply = &iod->supplies[i];
521
522                 if (io_supply->reg)
523                         regulator_unregister_notifier(io_supply->reg,
524                                                       &io_supply->nb);
525         }
526
527         return ret;
528 }
529
530 static int rockchip_iodomain_remove(struct platform_device *pdev)
531 {
532         struct rockchip_iodomain *iod = platform_get_drvdata(pdev);
533         int i;
534
535         for (i = MAX_SUPPLIES - 1; i >= 0; i--) {
536                 struct rockchip_iodomain_supply *io_supply = &iod->supplies[i];
537
538                 if (io_supply->reg)
539                         regulator_unregister_notifier(io_supply->reg,
540                                                       &io_supply->nb);
541         }
542
543         return 0;
544 }
545
546 static struct platform_driver rockchip_iodomain_driver = {
547         .probe   = rockchip_iodomain_probe,
548         .remove  = rockchip_iodomain_remove,
549         .driver  = {
550                 .name  = "rockchip-iodomain",
551                 .of_match_table = rockchip_iodomain_match,
552         },
553 };
554
555 module_platform_driver(rockchip_iodomain_driver);
556
557 MODULE_DESCRIPTION("Rockchip IO-domain driver");
558 MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
559 MODULE_AUTHOR("Doug Anderson <dianders@chromium.org>");
560 MODULE_LICENSE("GPL v2");