5d2bfe2ea9717ad1ddb3ff14fe75b1430d86d45e
[linux-2.6-microblaze.git] / drivers / staging / media / atomisp / pci / atomisp_gmin_platform.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/module.h>
3 #include <linux/i2c.h>
4 #include <linux/dmi.h>
5 #include <linux/efi.h>
6 #include <linux/pci.h>
7 #include <linux/acpi.h>
8 #include <linux/clk.h>
9 #include <linux/delay.h>
10 #include <media/v4l2-subdev.h>
11 #include <linux/mfd/intel_soc_pmic.h>
12 #include <linux/regulator/consumer.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/gpio.h>
15 #include <linux/platform_device.h>
16 #include "../../include/linux/atomisp_platform.h"
17 #include "../../include/linux/atomisp_gmin_platform.h"
18
19 #define MAX_SUBDEVS 8
20
21 enum clock_rate {
22         VLV2_CLK_XTAL_25_0MHz = 0,
23         VLV2_CLK_PLL_19P2MHZ = 1
24 };
25
26 #define CLK_RATE_19_2MHZ        19200000
27 #define CLK_RATE_25_0MHZ        25000000
28
29 /* X-Powers AXP288 register set */
30 #define ALDO1_SEL_REG   0x28
31 #define ALDO1_CTRL3_REG 0x13
32 #define ALDO1_2P8V      0x16
33 #define ALDO1_CTRL3_SHIFT 0x05
34
35 #define ELDO_CTRL_REG   0x12
36
37 #define ELDO1_SEL_REG   0x19
38 #define ELDO1_1P8V      0x16
39 #define ELDO1_CTRL_SHIFT 0x00
40
41 #define ELDO2_SEL_REG   0x1a
42 #define ELDO2_1P8V      0x16
43 #define ELDO2_CTRL_SHIFT 0x01
44
45 /* TI SND9039 PMIC register set */
46 #define LDO9_REG        0x49
47 #define LDO10_REG       0x4a
48 #define LDO11_REG       0x4b
49
50 #define LDO_2P8V_ON     0x2f /* 0x2e selects 2.85V ...      */
51 #define LDO_2P8V_OFF    0x2e /* ... bottom bit is "enabled" */
52
53 #define LDO_1P8V_ON     0x59 /* 0x58 selects 1.80V ...      */
54 #define LDO_1P8V_OFF    0x58 /* ... bottom bit is "enabled" */
55
56 /* CRYSTAL COVE PMIC register set */
57 #define CRYSTAL_1P8V_REG        0x57
58 #define CRYSTAL_2P8V_REG        0x5d
59 #define CRYSTAL_ON              0x63
60 #define CRYSTAL_OFF             0x62
61
62 struct gmin_subdev {
63         struct v4l2_subdev *subdev;
64         int clock_num;
65         enum clock_rate clock_src;
66         bool clock_on;
67         struct clk *pmc_clk;
68         struct gpio_desc *gpio0;
69         struct gpio_desc *gpio1;
70         struct regulator *v1p8_reg;
71         struct regulator *v2p8_reg;
72         struct regulator *v1p2_reg;
73         struct regulator *v2p8_vcm_reg;
74         enum atomisp_camera_port csi_port;
75         unsigned int csi_lanes;
76         enum atomisp_input_format csi_fmt;
77         enum atomisp_bayer_order csi_bayer;
78         bool v1p8_on;
79         bool v2p8_on;
80         bool v1p2_on;
81         bool v2p8_vcm_on;
82
83         u8 pwm_i2c_addr;
84
85         /* For PMIC AXP */
86         int eldo1_sel_reg, eldo1_1p8v, eldo1_ctrl_shift;
87         int eldo2_sel_reg, eldo2_1p8v, eldo2_ctrl_shift;
88 };
89
90 static struct gmin_subdev gmin_subdevs[MAX_SUBDEVS];
91
92 /* ACPI HIDs for the PMICs that could be used by this driver */
93 #define PMIC_ACPI_AXP           "INT33F4:00"    /* XPower AXP288 PMIC */
94 #define PMIC_ACPI_TI            "INT33F5:00"    /* Dollar Cove TI PMIC */
95 #define PMIC_ACPI_CRYSTALCOVE   "INT33FD:00"    /* Crystal Cove PMIC */
96
97 #define PMIC_PLATFORM_TI        "intel_soc_pmic_chtdc_ti"
98
99 static enum {
100         PMIC_UNSET = 0,
101         PMIC_REGULATOR,
102         PMIC_AXP,
103         PMIC_TI,
104         PMIC_CRYSTALCOVE
105 } pmic_id;
106
107 static const char *pmic_name[] = {
108         [PMIC_UNSET]            = "unset",
109         [PMIC_REGULATOR]        = "regulator driver",
110         [PMIC_AXP]              = "XPower AXP288 PMIC",
111         [PMIC_TI]               = "Dollar Cove TI PMIC",
112         [PMIC_CRYSTALCOVE]      = "Crystal Cove PMIC",
113 };
114
115 /* The atomisp uses type==0 for the end-of-list marker, so leave space. */
116 static struct intel_v4l2_subdev_table pdata_subdevs[MAX_SUBDEVS + 1];
117
118 static const struct atomisp_platform_data pdata = {
119         .subdevs = pdata_subdevs,
120 };
121
122 /*
123  * Something of a hack.  The ECS E7 board drives camera 2.8v from an
124  * external regulator instead of the PMIC.  There's a gmin_CamV2P8
125  * config variable that specifies the GPIO to handle this particular
126  * case, but this needs a broader architecture for handling camera
127  * power.
128  */
129 enum { V2P8_GPIO_UNSET = -2, V2P8_GPIO_NONE = -1 };
130 static int v2p8_gpio = V2P8_GPIO_UNSET;
131
132 /*
133  * Something of a hack. The CHT RVP board drives camera 1.8v from an
134  * external regulator instead of the PMIC just like ECS E7 board, see the
135  * comments above.
136  */
137 enum { V1P8_GPIO_UNSET = -2, V1P8_GPIO_NONE = -1 };
138 static int v1p8_gpio = V1P8_GPIO_UNSET;
139
140 static LIST_HEAD(vcm_devices);
141 static DEFINE_MUTEX(vcm_lock);
142
143 static struct gmin_subdev *find_gmin_subdev(struct v4l2_subdev *subdev);
144
145 /*
146  * Legacy/stub behavior copied from upstream platform_camera.c.  The
147  * atomisp driver relies on these values being non-NULL in a few
148  * places, even though they are hard-coded in all current
149  * implementations.
150  */
151 const struct atomisp_camera_caps *atomisp_get_default_camera_caps(void)
152 {
153         static const struct atomisp_camera_caps caps = {
154                 .sensor_num = 1,
155                 .sensor = {
156                         { .stream_num = 1, },
157                 },
158         };
159         return &caps;
160 }
161 EXPORT_SYMBOL_GPL(atomisp_get_default_camera_caps);
162
163 const struct atomisp_platform_data *atomisp_get_platform_data(void)
164 {
165         return &pdata;
166 }
167 EXPORT_SYMBOL_GPL(atomisp_get_platform_data);
168
169 int atomisp_register_i2c_module(struct v4l2_subdev *subdev,
170                                 struct camera_sensor_platform_data *plat_data,
171                                 enum intel_v4l2_subdev_type type)
172 {
173         int i;
174         struct i2c_board_info *bi;
175         struct gmin_subdev *gs;
176         struct i2c_client *client = v4l2_get_subdevdata(subdev);
177         struct acpi_device *adev = ACPI_COMPANION(&client->dev);
178
179         dev_info(&client->dev, "register atomisp i2c module type %d\n", type);
180
181         /* The windows driver model (and thus most BIOSes by default)
182          * uses ACPI runtime power management for camera devices, but
183          * we don't.  Disable it, or else the rails will be needlessly
184          * tickled during suspend/resume.  This has caused power and
185          * performance issues on multiple devices.
186          */
187         adev->power.flags.power_resources = 0;
188
189         for (i = 0; i < MAX_SUBDEVS; i++)
190                 if (!pdata.subdevs[i].type)
191                         break;
192
193         if (pdata.subdevs[i].type)
194                 return -ENOMEM;
195
196         /* Note subtlety of initialization order: at the point where
197          * this registration API gets called, the platform data
198          * callbacks have probably already been invoked, so the
199          * gmin_subdev struct is already initialized for us.
200          */
201         gs = find_gmin_subdev(subdev);
202
203         pdata.subdevs[i].type = type;
204         pdata.subdevs[i].port = gs->csi_port;
205         pdata.subdevs[i].subdev = subdev;
206         pdata.subdevs[i].v4l2_subdev.i2c_adapter_id = client->adapter->nr;
207
208         /* Convert i2c_client to i2c_board_info */
209         bi = &pdata.subdevs[i].v4l2_subdev.board_info;
210         memcpy(bi->type, client->name, I2C_NAME_SIZE);
211         bi->flags = client->flags;
212         bi->addr = client->addr;
213         bi->irq = client->irq;
214         bi->platform_data = plat_data;
215
216         return 0;
217 }
218 EXPORT_SYMBOL_GPL(atomisp_register_i2c_module);
219
220 struct v4l2_subdev *atomisp_gmin_find_subdev(struct i2c_adapter *adapter,
221         struct i2c_board_info *board_info)
222 {
223         int i;
224
225         for (i = 0; i < MAX_SUBDEVS && pdata.subdevs[i].type; i++) {
226                 struct intel_v4l2_subdev_table *sd = &pdata.subdevs[i];
227
228                 if (sd->v4l2_subdev.i2c_adapter_id == adapter->nr &&
229                     sd->v4l2_subdev.board_info.addr == board_info->addr)
230                         return sd->subdev;
231         }
232         return NULL;
233 }
234 EXPORT_SYMBOL_GPL(atomisp_gmin_find_subdev);
235
236 int atomisp_gmin_remove_subdev(struct v4l2_subdev *sd)
237 {
238         int i, j;
239
240         if (!sd)
241                 return 0;
242
243         for (i = 0; i < MAX_SUBDEVS; i++) {
244                 if (pdata.subdevs[i].subdev == sd) {
245                         for (j = i + 1; j <= MAX_SUBDEVS; j++)
246                                 pdata.subdevs[j - 1] = pdata.subdevs[j];
247                 }
248                 if (gmin_subdevs[i].subdev == sd) {
249                         if (gmin_subdevs[i].gpio0)
250                                 gpiod_put(gmin_subdevs[i].gpio0);
251                         gmin_subdevs[i].gpio0 = NULL;
252                         if (gmin_subdevs[i].gpio1)
253                                 gpiod_put(gmin_subdevs[i].gpio1);
254                         gmin_subdevs[i].gpio1 = NULL;
255                         if (pmic_id == PMIC_REGULATOR) {
256                                 regulator_put(gmin_subdevs[i].v1p8_reg);
257                                 regulator_put(gmin_subdevs[i].v2p8_reg);
258                                 regulator_put(gmin_subdevs[i].v1p2_reg);
259                                 regulator_put(gmin_subdevs[i].v2p8_vcm_reg);
260                         }
261                         gmin_subdevs[i].subdev = NULL;
262                 }
263         }
264         return 0;
265 }
266 EXPORT_SYMBOL_GPL(atomisp_gmin_remove_subdev);
267
268 struct gmin_cfg_var {
269         const char *name, *val;
270 };
271
272 static struct gmin_cfg_var ffrd8_vars[] = {
273         { "INTCF1B:00_ImxId",    "0x134" },
274         { "INTCF1B:00_CsiPort",  "1" },
275         { "INTCF1B:00_CsiLanes", "4" },
276         { "INTCF1B:00_CamClk", "0" },
277         {},
278 };
279
280 /* Cribbed from MCG defaults in the mt9m114 driver, not actually verified
281  * vs. T100 hardware
282  */
283 static struct gmin_cfg_var t100_vars[] = {
284         { "INT33F0:00_CsiPort",  "0" },
285         { "INT33F0:00_CsiLanes", "1" },
286         { "INT33F0:00_CamClk",   "1" },
287         {},
288 };
289
290 static struct gmin_cfg_var mrd7_vars[] = {
291         {"INT33F8:00_CamType", "1"},
292         {"INT33F8:00_CsiPort", "1"},
293         {"INT33F8:00_CsiLanes", "2"},
294         {"INT33F8:00_CsiFmt", "13"},
295         {"INT33F8:00_CsiBayer", "0"},
296         {"INT33F8:00_CamClk", "0"},
297         {"INT33F9:00_CamType", "1"},
298         {"INT33F9:00_CsiPort", "0"},
299         {"INT33F9:00_CsiLanes", "1"},
300         {"INT33F9:00_CsiFmt", "13"},
301         {"INT33F9:00_CsiBayer", "0"},
302         {"INT33F9:00_CamClk", "1"},
303         {},
304 };
305
306 static struct gmin_cfg_var ecs7_vars[] = {
307         {"INT33BE:00_CsiPort", "1"},
308         {"INT33BE:00_CsiLanes", "2"},
309         {"INT33BE:00_CsiFmt", "13"},
310         {"INT33BE:00_CsiBayer", "2"},
311         {"INT33BE:00_CamClk", "0"},
312         {"INT33F0:00_CsiPort", "0"},
313         {"INT33F0:00_CsiLanes", "1"},
314         {"INT33F0:00_CsiFmt", "13"},
315         {"INT33F0:00_CsiBayer", "0"},
316         {"INT33F0:00_CamClk", "1"},
317         {"gmin_V2P8GPIO", "402"},
318         {},
319 };
320
321 static struct gmin_cfg_var i8880_vars[] = {
322         {"XXOV2680:00_CsiPort", "1"},
323         {"XXOV2680:00_CsiLanes", "1"},
324         {"XXOV2680:00_CamClk", "0"},
325         {"XXGC0310:00_CsiPort", "0"},
326         {"XXGC0310:00_CsiLanes", "1"},
327         {"XXGC0310:00_CamClk", "1"},
328         {},
329 };
330
331 static struct gmin_cfg_var asus_vars[] = {
332         {"OVTI2680:00_CsiPort", "1"},
333         {"OVTI2680:00_CsiLanes", "1"},
334         {"OVTI2680:00_CsiFmt", "15"},
335         {"OVTI2680:00_CsiBayer", "0"},
336         {"OVTI2680:00_CamClk", "1"},
337         {},
338 };
339
340 static const struct dmi_system_id gmin_vars[] = {
341         {
342                 .ident = "BYT-T FFD8",
343                 .matches = {
344                         DMI_MATCH(DMI_BOARD_NAME, "BYT-T FFD8"),
345                 },
346                 .driver_data = ffrd8_vars,
347         },
348         {
349                 .ident = "T100TA",
350                 .matches = {
351                         DMI_MATCH(DMI_BOARD_NAME, "T100TA"),
352                 },
353                 .driver_data = t100_vars,
354         },
355         {
356                 .ident = "MRD7",
357                 .matches = {
358                         DMI_MATCH(DMI_BOARD_NAME, "TABLET"),
359                         DMI_MATCH(DMI_BOARD_VERSION, "MRD 7"),
360                 },
361                 .driver_data = mrd7_vars,
362         },
363         {
364                 .ident = "ST70408",
365                 .matches = {
366                         DMI_MATCH(DMI_BOARD_NAME, "ST70408"),
367                 },
368                 .driver_data = ecs7_vars,
369         },
370         {
371                 .ident = "VTA0803",
372                 .matches = {
373                         DMI_MATCH(DMI_BOARD_NAME, "VTA0803"),
374                 },
375                 .driver_data = i8880_vars,
376         },
377         {
378                 .ident = "T101HA",
379                 .matches = {
380                         DMI_MATCH(DMI_BOARD_NAME, "T101HA"),
381                 },
382                 .driver_data = asus_vars,
383         },
384         {}
385 };
386
387 #define GMIN_CFG_VAR_EFI_GUID EFI_GUID(0xecb54cd9, 0xe5ae, 0x4fdc, \
388                                        0xa9, 0x71, 0xe8, 0x77,     \
389                                        0x75, 0x60, 0x68, 0xf7)
390
391 #define CFG_VAR_NAME_MAX 64
392
393 #define GMIN_PMC_CLK_NAME 14 /* "pmc_plt_clk_[0..5]" */
394 static char gmin_pmc_clk_name[GMIN_PMC_CLK_NAME];
395
396 static int gmin_i2c_match_one(struct device *dev, const void *data)
397 {
398         const char *name = data;
399         struct i2c_client *client;
400
401         if (dev->type != &i2c_client_type)
402                 return 0;
403
404         client = to_i2c_client(dev);
405
406         return (!strcmp(name, client->name));
407 }
408
409 static struct i2c_client *gmin_i2c_dev_exists(struct device *dev, char *name,
410                                               struct i2c_client **client)
411 {
412         struct device *d;
413
414         while ((d = bus_find_device(&i2c_bus_type, NULL, name,
415                                     gmin_i2c_match_one))) {
416                 *client = to_i2c_client(d);
417                 dev_dbg(dev, "found '%s' at address 0x%02x, adapter %d\n",
418                         (*client)->name, (*client)->addr,
419                         (*client)->adapter->nr);
420                 return *client;
421         }
422
423         return NULL;
424 }
425
426 static int gmin_i2c_write(struct device *dev, u16 i2c_addr, u8 reg,
427                           u32 value, u32 mask)
428 {
429         int ret;
430
431         /*
432          * FIXME: Right now, the intel_pmic driver just write values
433          * directly at the regmap, instead of properly implementing
434          * i2c_transfer() mechanism. Let's use the same interface here,
435          * as otherwise we may face issues.
436          */
437
438         dev_dbg(dev,
439                 "I2C write, addr: 0x%02x, reg: 0x%02x, value: 0x%02x, mask: 0x%02x\n",
440                 i2c_addr, reg, value, mask);
441
442         ret = intel_soc_pmic_exec_mipi_pmic_seq_element(i2c_addr, reg,
443                                                         value, mask);
444
445         if (ret == -EOPNOTSUPP) {
446                 dev_err(dev,
447                         "ACPI didn't mapped the OpRegion needed to access I2C address 0x%02x.\n"
448                         "Need to compile the Kernel using CONFIG_*_PMIC_OPREGION settings\n",
449                         i2c_addr);
450                 return ret;
451         }
452
453         return ret;
454 }
455
456 static struct gmin_subdev *gmin_subdev_add(struct v4l2_subdev *subdev)
457 {
458         int i, ret;
459         struct device *dev;
460         struct i2c_client *power = NULL, *client = v4l2_get_subdevdata(subdev);
461
462         if (!client)
463                 return NULL;
464
465         dev = &client->dev;
466
467         if (!pmic_id) {
468                 if (gmin_i2c_dev_exists(dev, PMIC_ACPI_TI, &power))
469                         pmic_id = PMIC_TI;
470                 else if (gmin_i2c_dev_exists(dev, PMIC_ACPI_AXP, &power))
471                         pmic_id = PMIC_AXP;
472                 else if (gmin_i2c_dev_exists(dev, PMIC_ACPI_CRYSTALCOVE, &power))
473                         pmic_id = PMIC_CRYSTALCOVE;
474                 else
475                         pmic_id = PMIC_REGULATOR;
476         }
477
478         for (i = 0; i < MAX_SUBDEVS && gmin_subdevs[i].subdev; i++)
479                 ;
480         if (i >= MAX_SUBDEVS)
481                 return NULL;
482
483         if (power) {
484                 gmin_subdevs[i].pwm_i2c_addr = power->addr;
485                 dev_info(dev,
486                          "gmin: power management provided via %s (i2c addr 0x%02x)\n",
487                          pmic_name[pmic_id], power->addr);
488         } else {
489                 dev_info(dev, "gmin: power management provided via %s\n",
490                          pmic_name[pmic_id]);
491         }
492
493         gmin_subdevs[i].subdev = subdev;
494         gmin_subdevs[i].clock_num = gmin_get_var_int(dev, false, "CamClk", 0);
495         /*WA:CHT requires XTAL clock as PLL is not stable.*/
496         gmin_subdevs[i].clock_src = gmin_get_var_int(dev, false, "ClkSrc",
497                                     VLV2_CLK_PLL_19P2MHZ);
498         gmin_subdevs[i].csi_port = gmin_get_var_int(dev, false, "CsiPort", 0);
499         gmin_subdevs[i].csi_lanes = gmin_get_var_int(dev, false, "CsiLanes", 1);
500
501         /* get PMC clock with clock framework */
502         snprintf(gmin_pmc_clk_name,
503                  sizeof(gmin_pmc_clk_name),
504                  "%s_%d", "pmc_plt_clk", gmin_subdevs[i].clock_num);
505
506         gmin_subdevs[i].pmc_clk = devm_clk_get(dev, gmin_pmc_clk_name);
507         if (IS_ERR(gmin_subdevs[i].pmc_clk)) {
508                 ret = PTR_ERR(gmin_subdevs[i].pmc_clk);
509
510                 dev_err(dev,
511                         "Failed to get clk from %s : %d\n",
512                         gmin_pmc_clk_name,
513                         ret);
514
515                 return NULL;
516         }
517
518         /*
519          * The firmware might enable the clock at
520          * boot (this information may or may not
521          * be reflected in the enable clock register).
522          * To change the rate we must disable the clock
523          * first to cover these cases. Due to common
524          * clock framework restrictions that do not allow
525          * to disable a clock that has not been enabled,
526          * we need to enable the clock first.
527          */
528         ret = clk_prepare_enable(gmin_subdevs[i].pmc_clk);
529         if (!ret)
530                 clk_disable_unprepare(gmin_subdevs[i].pmc_clk);
531
532         gmin_subdevs[i].gpio0 = gpiod_get_index(dev, NULL, 0, GPIOD_OUT_LOW);
533         if (IS_ERR(gmin_subdevs[i].gpio0))
534                 gmin_subdevs[i].gpio0 = NULL;
535
536         gmin_subdevs[i].gpio1 = gpiod_get_index(dev, NULL, 1, GPIOD_OUT_LOW);
537         if (IS_ERR(gmin_subdevs[i].gpio1))
538                 gmin_subdevs[i].gpio1 = NULL;
539
540         switch (pmic_id) {
541         case PMIC_REGULATOR:
542                 gmin_subdevs[i].v1p8_reg = regulator_get(dev, "V1P8SX");
543                 gmin_subdevs[i].v2p8_reg = regulator_get(dev, "V2P8SX");
544
545                 gmin_subdevs[i].v1p2_reg = regulator_get(dev, "V1P2A");
546                 gmin_subdevs[i].v2p8_vcm_reg = regulator_get(dev, "VPROG4B");
547
548                 /* Note: ideally we would initialize v[12]p8_on to the
549                  * output of regulator_is_enabled(), but sadly that
550                  * API is broken with the current drivers, returning
551                  * "1" for a regulator that will then emit a
552                  * "unbalanced disable" WARNing if we try to disable
553                  * it.
554                  */
555                 break;
556
557         case PMIC_AXP:
558                 gmin_subdevs[i].eldo1_1p8v = gmin_get_var_int(dev, false,
559                                                               "eldo1_1p8v",
560                                                               ELDO1_1P8V);
561                 gmin_subdevs[i].eldo1_sel_reg = gmin_get_var_int(dev, false,
562                                                                  "eldo1_sel_reg",
563                                                                  ELDO1_SEL_REG);
564                 gmin_subdevs[i].eldo1_ctrl_shift = gmin_get_var_int(dev, false,
565                                                                     "eldo1_ctrl_shift",
566                                                                     ELDO1_CTRL_SHIFT);
567                 gmin_subdevs[i].eldo2_1p8v = gmin_get_var_int(dev, false,
568                                                               "eldo2_1p8v",
569                                                               ELDO2_1P8V);
570                 gmin_subdevs[i].eldo2_sel_reg = gmin_get_var_int(dev, false,
571                                                                  "eldo2_sel_reg",
572                                                                  ELDO2_SEL_REG);
573                 gmin_subdevs[i].eldo2_ctrl_shift = gmin_get_var_int(dev, false,
574                                                                     "eldo2_ctrl_shift",
575                                                                     ELDO2_CTRL_SHIFT);
576                 gmin_subdevs[i].pwm_i2c_addr = power->addr;
577                 break;
578
579         default:
580                 break;
581         }
582
583         return &gmin_subdevs[i];
584 }
585
586 static struct gmin_subdev *find_gmin_subdev(struct v4l2_subdev *subdev)
587 {
588         int i;
589
590         for (i = 0; i < MAX_SUBDEVS; i++)
591                 if (gmin_subdevs[i].subdev == subdev)
592                         return &gmin_subdevs[i];
593         return gmin_subdev_add(subdev);
594 }
595
596 static int axp_regulator_set(struct device *dev, struct gmin_subdev *gs,
597                              int sel_reg, u8 setting,
598                              int ctrl_reg, int shift, bool on)
599 {
600         int ret;
601         int val;
602
603         ret = gmin_i2c_write(dev, gs->pwm_i2c_addr, sel_reg, setting, 0xff);
604         if (ret)
605                 return ret;
606
607         val = on ? 1 << shift : 0;
608
609         ret = gmin_i2c_write(dev, gs->pwm_i2c_addr, sel_reg, val, 1 << shift);
610         if (ret)
611                 return ret;
612
613         return 0;
614 }
615
616 static int axp_v1p8_on(struct device *dev, struct gmin_subdev *gs)
617 {
618         int ret;
619
620         ret = axp_regulator_set(dev, gs, gs->eldo2_sel_reg, gs->eldo2_1p8v,
621                                 ELDO_CTRL_REG, gs->eldo2_ctrl_shift, true);
622         if (ret)
623                 return ret;
624
625         /*
626          * This sleep comes out of the gc2235 driver, which is the
627          * only one I currently see that wants to set both 1.8v rails.
628          */
629         usleep_range(110, 150);
630
631         ret = axp_regulator_set(dev, gs, gs->eldo1_sel_reg, gs->eldo1_1p8v,
632                 ELDO_CTRL_REG, gs->eldo1_ctrl_shift, true);
633         if (ret)
634                 return ret;
635
636         ret = axp_regulator_set(dev, gs, gs->eldo2_sel_reg, gs->eldo2_1p8v,
637                                 ELDO_CTRL_REG, gs->eldo2_ctrl_shift, false);
638         return ret;
639 }
640
641 static int axp_v1p8_off(struct device *dev, struct gmin_subdev *gs)
642 {
643         int ret;
644
645         ret = axp_regulator_set(dev, gs, gs->eldo1_sel_reg, gs->eldo1_1p8v,
646                                 ELDO_CTRL_REG, gs->eldo1_ctrl_shift, false);
647         if (ret)
648                 return ret;
649
650         ret = axp_regulator_set(dev, gs, gs->eldo2_sel_reg, gs->eldo2_1p8v,
651                                 ELDO_CTRL_REG, gs->eldo2_ctrl_shift, false);
652         return ret;
653 }
654
655 static int gmin_gpio0_ctrl(struct v4l2_subdev *subdev, int on)
656 {
657         struct gmin_subdev *gs = find_gmin_subdev(subdev);
658
659         if (gs) {
660                 gpiod_set_value(gs->gpio0, on);
661                 return 0;
662         }
663         return -EINVAL;
664 }
665
666 static int gmin_gpio1_ctrl(struct v4l2_subdev *subdev, int on)
667 {
668         struct gmin_subdev *gs = find_gmin_subdev(subdev);
669
670         if (gs) {
671                 gpiod_set_value(gs->gpio1, on);
672                 return 0;
673         }
674         return -EINVAL;
675 }
676
677 static int gmin_v1p2_ctrl(struct v4l2_subdev *subdev, int on)
678 {
679         struct gmin_subdev *gs = find_gmin_subdev(subdev);
680
681         if (!gs || gs->v1p2_on == on)
682                 return 0;
683         gs->v1p2_on = on;
684
685         /* use regulator for PMIC */
686         if (gs->v1p2_reg) {
687                 if (on)
688                         return regulator_enable(gs->v1p2_reg);
689                 else
690                         return regulator_disable(gs->v1p2_reg);
691         }
692
693         /* TODO:v1p2 may need to extend to other PMICs */
694
695         return -EINVAL;
696 }
697
698 static int gmin_v1p8_ctrl(struct v4l2_subdev *subdev, int on)
699 {
700         struct gmin_subdev *gs = find_gmin_subdev(subdev);
701         int ret;
702         struct device *dev;
703         struct i2c_client *client = v4l2_get_subdevdata(subdev);
704         int value;
705
706         dev = &client->dev;
707
708         if (v1p8_gpio == V1P8_GPIO_UNSET) {
709                 v1p8_gpio = gmin_get_var_int(dev, true,
710                                              "V1P8GPIO", V1P8_GPIO_NONE);
711                 if (v1p8_gpio != V1P8_GPIO_NONE) {
712                         pr_info("atomisp_gmin_platform: 1.8v power on GPIO %d\n",
713                                 v1p8_gpio);
714                         ret = gpio_request(v1p8_gpio, "camera_v1p8_en");
715                         if (!ret)
716                                 ret = gpio_direction_output(v1p8_gpio, 0);
717                         if (ret)
718                                 pr_err("V1P8 GPIO initialization failed\n");
719                 }
720         }
721
722         if (!gs || gs->v1p8_on == on)
723                 return 0;
724         gs->v1p8_on = on;
725
726         if (v1p8_gpio >= 0)
727                 gpio_set_value(v1p8_gpio, on);
728
729         if (gs->v1p8_reg) {
730                 regulator_set_voltage(gs->v1p8_reg, 1800000, 1800000);
731                 if (on)
732                         return regulator_enable(gs->v1p8_reg);
733                 else
734                         return regulator_disable(gs->v1p8_reg);
735         }
736
737         switch (pmic_id) {
738         case PMIC_AXP:
739                 if (on)
740                         return axp_v1p8_on(subdev->dev, gs);
741                 else
742                         return axp_v1p8_off(subdev->dev, gs);
743         case PMIC_TI:
744                 value = on ? LDO_1P8V_ON : LDO_1P8V_OFF;
745
746                 return gmin_i2c_write(subdev->dev, gs->pwm_i2c_addr,
747                                       LDO10_REG, value, 0xff);
748         case PMIC_CRYSTALCOVE:
749                 value = on ? CRYSTAL_ON : CRYSTAL_OFF;
750
751                 return gmin_i2c_write(subdev->dev, gs->pwm_i2c_addr,
752                                       CRYSTAL_1P8V_REG, value, 0xff);
753         default:
754                 dev_err(subdev->dev, "Couldn't set power mode for v1p2\n");
755         }
756
757         return -EINVAL;
758 }
759
760 static int gmin_v2p8_ctrl(struct v4l2_subdev *subdev, int on)
761 {
762         struct gmin_subdev *gs = find_gmin_subdev(subdev);
763         int ret;
764         struct device *dev;
765         struct i2c_client *client = v4l2_get_subdevdata(subdev);
766         int value;
767
768         dev = &client->dev;
769
770         if (v2p8_gpio == V2P8_GPIO_UNSET) {
771                 v2p8_gpio = gmin_get_var_int(dev, true,
772                                              "V2P8GPIO", V2P8_GPIO_NONE);
773                 if (v2p8_gpio != V2P8_GPIO_NONE) {
774                         pr_info("atomisp_gmin_platform: 2.8v power on GPIO %d\n",
775                                 v2p8_gpio);
776                         ret = gpio_request(v2p8_gpio, "camera_v2p8");
777                         if (!ret)
778                                 ret = gpio_direction_output(v2p8_gpio, 0);
779                         if (ret)
780                                 pr_err("V2P8 GPIO initialization failed\n");
781                 }
782         }
783
784         if (!gs || gs->v2p8_on == on)
785                 return 0;
786         gs->v2p8_on = on;
787
788         if (v2p8_gpio >= 0)
789                 gpio_set_value(v2p8_gpio, on);
790
791         if (gs->v2p8_reg) {
792                 regulator_set_voltage(gs->v2p8_reg, 2900000, 2900000);
793                 if (on)
794                         return regulator_enable(gs->v2p8_reg);
795                 else
796                         return regulator_disable(gs->v2p8_reg);
797         }
798
799         switch (pmic_id) {
800         case PMIC_AXP:
801                 return axp_regulator_set(subdev->dev, gs, ALDO1_SEL_REG,
802                                          ALDO1_2P8V, ALDO1_CTRL3_REG,
803                                          ALDO1_CTRL3_SHIFT, on);
804         case PMIC_TI:
805                 value = on ? LDO_2P8V_ON : LDO_2P8V_OFF;
806
807                 return gmin_i2c_write(subdev->dev, gs->pwm_i2c_addr,
808                                       LDO9_REG, value, 0xff);
809         case PMIC_CRYSTALCOVE:
810                 value = on ? CRYSTAL_ON : CRYSTAL_OFF;
811
812                 return gmin_i2c_write(subdev->dev, gs->pwm_i2c_addr,
813                                       CRYSTAL_2P8V_REG, value, 0xff);
814         default:
815                 dev_err(subdev->dev, "Couldn't set power mode for v1p2\n");
816         }
817
818         return -EINVAL;
819 }
820
821 static int gmin_flisclk_ctrl(struct v4l2_subdev *subdev, int on)
822 {
823         int ret = 0;
824         struct gmin_subdev *gs = find_gmin_subdev(subdev);
825         struct i2c_client *client = v4l2_get_subdevdata(subdev);
826
827         if (gs->clock_on == !!on)
828                 return 0;
829
830         if (on) {
831                 ret = clk_set_rate(gs->pmc_clk,
832                                    gs->clock_src ? CLK_RATE_19_2MHZ : CLK_RATE_25_0MHZ);
833
834                 if (ret)
835                         dev_err(&client->dev, "unable to set PMC rate %d\n",
836                                 gs->clock_src);
837
838                 ret = clk_prepare_enable(gs->pmc_clk);
839                 if (ret == 0)
840                         gs->clock_on = true;
841         } else {
842                 clk_disable_unprepare(gs->pmc_clk);
843                 gs->clock_on = false;
844         }
845
846         return ret;
847 }
848
849 static int gmin_csi_cfg(struct v4l2_subdev *sd, int flag)
850 {
851         struct i2c_client *client = v4l2_get_subdevdata(sd);
852         struct gmin_subdev *gs = find_gmin_subdev(sd);
853
854         if (!client || !gs)
855                 return -ENODEV;
856
857         return camera_sensor_csi(sd, gs->csi_port, gs->csi_lanes,
858                                  gs->csi_fmt, gs->csi_bayer, flag);
859 }
860
861 static struct camera_vcm_control *gmin_get_vcm_ctrl(struct v4l2_subdev *subdev,
862         char *camera_module)
863 {
864         struct i2c_client *client = v4l2_get_subdevdata(subdev);
865         struct gmin_subdev *gs = find_gmin_subdev(subdev);
866         struct camera_vcm_control *vcm;
867
868         if (!client || !gs)
869                 return NULL;
870
871         if (!camera_module)
872                 return NULL;
873
874         mutex_lock(&vcm_lock);
875         list_for_each_entry(vcm, &vcm_devices, list) {
876                 if (!strcmp(camera_module, vcm->camera_module)) {
877                         mutex_unlock(&vcm_lock);
878                         return vcm;
879                 }
880         }
881
882         mutex_unlock(&vcm_lock);
883         return NULL;
884 }
885
886 static struct camera_sensor_platform_data gmin_plat = {
887         .gpio0_ctrl = gmin_gpio0_ctrl,
888         .gpio1_ctrl = gmin_gpio1_ctrl,
889         .v1p8_ctrl = gmin_v1p8_ctrl,
890         .v2p8_ctrl = gmin_v2p8_ctrl,
891         .v1p2_ctrl = gmin_v1p2_ctrl,
892         .flisclk_ctrl = gmin_flisclk_ctrl,
893         .csi_cfg = gmin_csi_cfg,
894         .get_vcm_ctrl = gmin_get_vcm_ctrl,
895 };
896
897 struct camera_sensor_platform_data *gmin_camera_platform_data(
898     struct v4l2_subdev *subdev,
899     enum atomisp_input_format csi_format,
900     enum atomisp_bayer_order csi_bayer)
901 {
902         struct gmin_subdev *gs = find_gmin_subdev(subdev);
903
904         gs->csi_fmt = csi_format;
905         gs->csi_bayer = csi_bayer;
906
907         return &gmin_plat;
908 }
909 EXPORT_SYMBOL_GPL(gmin_camera_platform_data);
910
911 int atomisp_gmin_register_vcm_control(struct camera_vcm_control *vcmCtrl)
912 {
913         if (!vcmCtrl)
914                 return -EINVAL;
915
916         mutex_lock(&vcm_lock);
917         list_add_tail(&vcmCtrl->list, &vcm_devices);
918         mutex_unlock(&vcm_lock);
919
920         return 0;
921 }
922 EXPORT_SYMBOL_GPL(atomisp_gmin_register_vcm_control);
923
924 static int gmin_get_hardcoded_var(struct gmin_cfg_var *varlist,
925                                   const char *var8, char *out, size_t *out_len)
926 {
927         struct gmin_cfg_var *gv;
928
929         for (gv = varlist; gv->name; gv++) {
930                 size_t vl;
931
932                 if (strcmp(var8, gv->name))
933                         continue;
934
935                 vl = strlen(gv->val);
936                 if (vl > *out_len - 1)
937                         return -ENOSPC;
938
939                 strscpy(out, gv->val, *out_len);
940                 *out_len = vl;
941                 return 0;
942         }
943
944         return -EINVAL;
945 }
946
947 /* Retrieves a device-specific configuration variable.  The dev
948  * argument should be a device with an ACPI companion, as all
949  * configuration is based on firmware ID.
950  */
951 static int gmin_get_config_var(struct device *maindev,
952                                bool is_gmin,
953                                const char *var,
954                                char *out, size_t *out_len)
955 {
956         char var8[CFG_VAR_NAME_MAX];
957         efi_char16_t var16[CFG_VAR_NAME_MAX];
958         struct efivar_entry *ev;
959         const struct dmi_system_id *id;
960         int i, ret;
961         struct device *dev = maindev;
962
963         if (!is_gmin && ACPI_COMPANION(dev))
964                 dev = &ACPI_COMPANION(dev)->dev;
965
966         if (!is_gmin)
967                 ret = snprintf(var8, sizeof(var8), "%s_%s", dev_name(dev), var);
968         else
969                 ret = snprintf(var8, sizeof(var8), "gmin_%s", var);
970
971         if (ret < 0 || ret >= sizeof(var8) - 1)
972                 return -EINVAL;
973
974         /* First check a hard-coded list of board-specific variables.
975          * Some device firmwares lack the ability to set EFI variables at
976          * runtime.
977          */
978         id = dmi_first_match(gmin_vars);
979         if (id) {
980                 dev_info(maindev, "Found DMI entry for '%s'\n", var8);
981                 return gmin_get_hardcoded_var(id->driver_data, var8, out,
982                                               out_len);
983         }
984
985         /* Our variable names are ASCII by construction, but EFI names
986          * are wide chars.  Convert and zero-pad.
987          */
988         memset(var16, 0, sizeof(var16));
989         for (i = 0; i < sizeof(var8) && var8[i]; i++)
990                 var16[i] = var8[i];
991
992         /* Not sure this API usage is kosher; efivar_entry_get()'s
993          * implementation simply uses VariableName and VendorGuid from
994          * the struct and ignores the rest, but it seems like there
995          * ought to be an "official" efivar_entry registered
996          * somewhere?
997          */
998         ev = kzalloc(sizeof(*ev), GFP_KERNEL);
999         if (!ev)
1000                 return -ENOMEM;
1001         memcpy(&ev->var.VariableName, var16, sizeof(var16));
1002         ev->var.VendorGuid = GMIN_CFG_VAR_EFI_GUID;
1003         ev->var.DataSize = *out_len;
1004
1005         ret = efivar_entry_get(ev, &ev->var.Attributes,
1006                                &ev->var.DataSize, ev->var.Data);
1007         if (ret == 0) {
1008                 memcpy(out, ev->var.Data, ev->var.DataSize);
1009                 *out_len = ev->var.DataSize;
1010                 dev_info(maindev, "found EFI entry for '%s'\n", var8);
1011         } else if (is_gmin) {
1012                 dev_warn(maindev, "Failed to find gmin variable %s\n", var8);
1013         } else {
1014                 dev_warn(maindev, "Failed to find variable %s\n", var8);
1015         }
1016
1017         kfree(ev);
1018
1019         return ret;
1020 }
1021
1022 int gmin_get_var_int(struct device *dev, bool is_gmin, const char *var, int def)
1023 {
1024         char val[CFG_VAR_NAME_MAX];
1025         size_t len = sizeof(val);
1026         long result;
1027         int ret;
1028
1029         ret = gmin_get_config_var(dev, is_gmin, var, val, &len);
1030         if (!ret) {
1031                 val[len] = 0;
1032                 ret = kstrtol(val, 0, &result);
1033         }
1034
1035         return ret ? def : result;
1036 }
1037 EXPORT_SYMBOL_GPL(gmin_get_var_int);
1038
1039 int camera_sensor_csi(struct v4l2_subdev *sd, u32 port,
1040                       u32 lanes, u32 format, u32 bayer_order, int flag)
1041 {
1042         struct i2c_client *client = v4l2_get_subdevdata(sd);
1043         struct camera_mipi_info *csi = NULL;
1044
1045         if (flag) {
1046                 csi = kzalloc(sizeof(*csi), GFP_KERNEL);
1047                 if (!csi)
1048                         return -ENOMEM;
1049                 csi->port = port;
1050                 csi->num_lanes = lanes;
1051                 csi->input_format = format;
1052                 csi->raw_bayer_order = bayer_order;
1053                 v4l2_set_subdev_hostdata(sd, (void *)csi);
1054                 csi->metadata_format = ATOMISP_INPUT_FORMAT_EMBEDDED;
1055                 csi->metadata_effective_width = NULL;
1056                 dev_info(&client->dev,
1057                          "camera pdata: port: %d lanes: %d order: %8.8x\n",
1058                          port, lanes, bayer_order);
1059         } else {
1060                 csi = v4l2_get_subdev_hostdata(sd);
1061                 kfree(csi);
1062         }
1063
1064         return 0;
1065 }
1066 EXPORT_SYMBOL_GPL(camera_sensor_csi);
1067
1068 /* PCI quirk: The BYT ISP advertises PCI runtime PM but it doesn't
1069  * work.  Disable so the kernel framework doesn't hang the device
1070  * trying.  The driver itself does direct calls to the PUNIT to manage
1071  * ISP power.
1072  */
1073 static void isp_pm_cap_fixup(struct pci_dev *dev)
1074 {
1075         dev_info(&dev->dev, "Disabling PCI power management on camera ISP\n");
1076         dev->pm_cap = 0;
1077 }
1078 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x0f38, isp_pm_cap_fixup);
1079
1080 MODULE_DESCRIPTION("Ancillary routines for binding ACPI devices");
1081 MODULE_LICENSE("GPL");