GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / staging / media / atomisp / platform / intel-mid / atomisp_gmin_platform.c
1 #include <linux/module.h>
2 #include <linux/i2c.h>
3 #include <linux/dmi.h>
4 #include <linux/efi.h>
5 #include <linux/pci.h>
6 #include <linux/acpi.h>
7 #include <linux/delay.h>
8 #include <media/v4l2-subdev.h>
9 #include <linux/mfd/intel_soc_pmic.h>
10 #include "../../include/linux/vlv2_plat_clock.h"
11 #include <linux/regulator/consumer.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/gpio.h>
14 #include <linux/platform_device.h>
15 #include "../../include/linux/atomisp_platform.h"
16 #include "../../include/linux/atomisp_gmin_platform.h"
17
18 #define MAX_SUBDEVS 8
19
20 /* Should be defined in vlv2_plat_clock API, isn't: */
21 #define VLV2_CLK_PLL_19P2MHZ 1
22 #define VLV2_CLK_XTAL_19P2MHZ 0
23 #define VLV2_CLK_ON      1
24 #define VLV2_CLK_OFF     2
25 #define ELDO1_SEL_REG   0x19
26 #define ELDO1_1P8V      0x16
27 #define ELDO1_CTRL_SHIFT 0x00
28 #define ELDO2_SEL_REG   0x1a
29 #define ELDO2_1P8V      0x16
30 #define ELDO2_CTRL_SHIFT 0x01
31
32 struct gmin_subdev {
33         struct v4l2_subdev *subdev;
34         int clock_num;
35         int clock_src;
36         struct gpio_desc *gpio0;
37         struct gpio_desc *gpio1;
38         struct regulator *v1p8_reg;
39         struct regulator *v2p8_reg;
40         struct regulator *v1p2_reg;
41         struct regulator *v2p8_vcm_reg;
42         enum atomisp_camera_port csi_port;
43         unsigned int csi_lanes;
44         enum atomisp_input_format csi_fmt;
45         enum atomisp_bayer_order csi_bayer;
46         bool v1p8_on;
47         bool v2p8_on;
48         bool v1p2_on;
49         bool v2p8_vcm_on;
50 };
51
52 static struct gmin_subdev gmin_subdevs[MAX_SUBDEVS];
53
54 static enum { PMIC_UNSET = 0, PMIC_REGULATOR, PMIC_AXP, PMIC_TI,
55         PMIC_CRYSTALCOVE } pmic_id;
56
57 /* The atomisp uses type==0 for the end-of-list marker, so leave space. */
58 static struct intel_v4l2_subdev_table pdata_subdevs[MAX_SUBDEVS + 1];
59
60 static const struct atomisp_platform_data pdata = {
61         .subdevs = pdata_subdevs,
62 };
63
64 /*
65  * Something of a hack.  The ECS E7 board drives camera 2.8v from an
66  * external regulator instead of the PMIC.  There's a gmin_CamV2P8
67  * config variable that specifies the GPIO to handle this particular
68  * case, but this needs a broader architecture for handling camera
69  * power.
70  */
71 enum { V2P8_GPIO_UNSET = -2, V2P8_GPIO_NONE = -1 };
72 static int v2p8_gpio = V2P8_GPIO_UNSET;
73
74 /*
75  * Something of a hack. The CHT RVP board drives camera 1.8v from an
76  * external regulator instead of the PMIC just like ECS E7 board, see the
77  * comments above.
78  */
79 enum { V1P8_GPIO_UNSET = -2, V1P8_GPIO_NONE = -1 };
80 static int v1p8_gpio = V1P8_GPIO_UNSET;
81
82 static LIST_HEAD(vcm_devices);
83 static DEFINE_MUTEX(vcm_lock);
84
85 static struct gmin_subdev *find_gmin_subdev(struct v4l2_subdev *subdev);
86
87 /*
88  * Legacy/stub behavior copied from upstream platform_camera.c.  The
89  * atomisp driver relies on these values being non-NULL in a few
90  * places, even though they are hard-coded in all current
91  * implementations.
92  */
93 const struct atomisp_camera_caps *atomisp_get_default_camera_caps(void)
94 {
95         static const struct atomisp_camera_caps caps = {
96                 .sensor_num = 1,
97                 .sensor = {
98                         { .stream_num = 1, },
99                 },
100         };
101         return &caps;
102 }
103 EXPORT_SYMBOL_GPL(atomisp_get_default_camera_caps);
104
105 const struct atomisp_platform_data *atomisp_get_platform_data(void)
106 {
107         return &pdata;
108 }
109 EXPORT_SYMBOL_GPL(atomisp_get_platform_data);
110
111 static int af_power_ctrl(struct v4l2_subdev *subdev, int flag)
112 {
113         struct gmin_subdev *gs = find_gmin_subdev(subdev);
114
115         if (gs && gs->v2p8_vcm_on == flag)
116                 return 0;
117         gs->v2p8_vcm_on = flag;
118
119         /*
120          * The power here is used for dw9817,
121          * regulator is from rear sensor
122          */
123         if (gs->v2p8_vcm_reg) {
124                 if (flag)
125                         return regulator_enable(gs->v2p8_vcm_reg);
126                 else
127                         return regulator_disable(gs->v2p8_vcm_reg);
128         }
129         return 0;
130 }
131
132 /*
133  * Used in a handful of modules.  Focus motor control, I think.  Note
134  * that there is no configurability in the API, so this needs to be
135  * fixed where it is used.
136  *
137  * struct camera_af_platform_data {
138  *     int (*power_ctrl)(struct v4l2_subdev *subdev, int flag);
139  * };
140  *
141  * Note that the implementation in MCG platform_camera.c is stubbed
142  * out anyway (i.e. returns zero from the callback) on BYT.  So
143  * neither needed on gmin platforms or supported upstream.
144  */
145 const struct camera_af_platform_data *camera_get_af_platform_data(void)
146 {
147         static struct camera_af_platform_data afpd = {
148                 .power_ctrl = af_power_ctrl,
149         };
150         return &afpd;
151 }
152 EXPORT_SYMBOL_GPL(camera_get_af_platform_data);
153
154 int atomisp_register_i2c_module(struct v4l2_subdev *subdev,
155                                 struct camera_sensor_platform_data *plat_data,
156                                 enum intel_v4l2_subdev_type type)
157 {
158         int i;
159         struct i2c_board_info *bi;
160         struct gmin_subdev *gs;
161         struct i2c_client *client = v4l2_get_subdevdata(subdev);
162         struct acpi_device *adev;
163
164         dev_info(&client->dev, "register atomisp i2c module type %d\n", type);
165
166         /* The windows driver model (and thus most BIOSes by default)
167          * uses ACPI runtime power management for camera devices, but
168          * we don't.  Disable it, or else the rails will be needlessly
169          * tickled during suspend/resume.  This has caused power and
170          * performance issues on multiple devices.
171          */
172         adev = ACPI_COMPANION(&client->dev);
173         if (adev)
174                 adev->power.flags.power_resources = 0;
175
176         for (i = 0; i < MAX_SUBDEVS; i++)
177                 if (!pdata.subdevs[i].type)
178                         break;
179
180         if (pdata.subdevs[i].type)
181                 return -ENOMEM;
182
183         /* Note subtlety of initialization order: at the point where
184          * this registration API gets called, the platform data
185          * callbacks have probably already been invoked, so the
186          * gmin_subdev struct is already initialized for us.
187          */
188         gs = find_gmin_subdev(subdev);
189
190         pdata.subdevs[i].type = type;
191         pdata.subdevs[i].port = gs->csi_port;
192         pdata.subdevs[i].subdev = subdev;
193         pdata.subdevs[i].v4l2_subdev.i2c_adapter_id = client->adapter->nr;
194
195         /* Convert i2c_client to i2c_board_info */
196         bi = &pdata.subdevs[i].v4l2_subdev.board_info;
197         memcpy(bi->type, client->name, I2C_NAME_SIZE);
198         bi->flags = client->flags;
199         bi->addr = client->addr;
200         bi->irq = client->irq;
201         bi->platform_data = plat_data;
202
203         return 0;
204 }
205 EXPORT_SYMBOL_GPL(atomisp_register_i2c_module);
206
207 struct v4l2_subdev *atomisp_gmin_find_subdev(struct i2c_adapter *adapter,
208                                              struct i2c_board_info *board_info)
209 {
210         int i;
211
212         for (i = 0; i < MAX_SUBDEVS && pdata.subdevs[i].type; i++) {
213                 struct intel_v4l2_subdev_table *sd = &pdata.subdevs[i];
214
215                 if (sd->v4l2_subdev.i2c_adapter_id == adapter->nr &&
216                     sd->v4l2_subdev.board_info.addr == board_info->addr)
217                         return sd->subdev;
218         }
219         return NULL;
220 }
221 EXPORT_SYMBOL_GPL(atomisp_gmin_find_subdev);
222
223 int atomisp_gmin_remove_subdev(struct v4l2_subdev *sd)
224 {
225         int i, j;
226
227         if (!sd)
228                 return 0;
229
230         for (i = 0; i < MAX_SUBDEVS; i++) {
231                 if (pdata.subdevs[i].subdev == sd) {
232                         for (j = i + 1; j <= MAX_SUBDEVS; j++)
233                                 pdata.subdevs[j - 1] = pdata.subdevs[j];
234                 }
235                 if (gmin_subdevs[i].subdev == sd) {
236                         if (gmin_subdevs[i].gpio0)
237                                 gpiod_put(gmin_subdevs[i].gpio0);
238                         gmin_subdevs[i].gpio0 = NULL;
239                         if (gmin_subdevs[i].gpio1)
240                                 gpiod_put(gmin_subdevs[i].gpio1);
241                         gmin_subdevs[i].gpio1 = NULL;
242                         if (pmic_id == PMIC_REGULATOR) {
243                                 regulator_put(gmin_subdevs[i].v1p8_reg);
244                                 regulator_put(gmin_subdevs[i].v2p8_reg);
245                                 regulator_put(gmin_subdevs[i].v1p2_reg);
246                                 regulator_put(gmin_subdevs[i].v2p8_vcm_reg);
247                         }
248                         gmin_subdevs[i].subdev = NULL;
249                 }
250         }
251         return 0;
252 }
253 EXPORT_SYMBOL_GPL(atomisp_gmin_remove_subdev);
254
255 struct gmin_cfg_var {
256         const char *name, *val;
257 };
258
259 static const struct gmin_cfg_var ffrd8_vars[] = {
260         { "INTCF1B:00_ImxId",    "0x134" },
261         { "INTCF1B:00_CsiPort",  "1" },
262         { "INTCF1B:00_CsiLanes", "4" },
263         { "INTCF1B:00_CamClk", "0" },
264         {},
265 };
266
267 /* Cribbed from MCG defaults in the mt9m114 driver, not actually verified
268  * vs. T100 hardware
269  */
270 static const struct gmin_cfg_var t100_vars[] = {
271         { "INT33F0:00_CsiPort",  "0" },
272         { "INT33F0:00_CsiLanes", "1" },
273         { "INT33F0:00_CamClk",   "1" },
274         {},
275 };
276
277 static const struct gmin_cfg_var mrd7_vars[] = {
278         {"INT33F8:00_CamType", "1"},
279         {"INT33F8:00_CsiPort", "1"},
280         {"INT33F8:00_CsiLanes", "2"},
281         {"INT33F8:00_CsiFmt", "13"},
282         {"INT33F8:00_CsiBayer", "0"},
283         {"INT33F8:00_CamClk", "0"},
284         {"INT33F9:00_CamType", "1"},
285         {"INT33F9:00_CsiPort", "0"},
286         {"INT33F9:00_CsiLanes", "1"},
287         {"INT33F9:00_CsiFmt", "13"},
288         {"INT33F9:00_CsiBayer", "0"},
289         {"INT33F9:00_CamClk", "1"},
290         {},
291 };
292
293 static const struct gmin_cfg_var ecs7_vars[] = {
294         {"INT33BE:00_CsiPort", "1"},
295         {"INT33BE:00_CsiLanes", "2"},
296         {"INT33BE:00_CsiFmt", "13"},
297         {"INT33BE:00_CsiBayer", "2"},
298         {"INT33BE:00_CamClk", "0"},
299         {"INT33F0:00_CsiPort", "0"},
300         {"INT33F0:00_CsiLanes", "1"},
301         {"INT33F0:00_CsiFmt", "13"},
302         {"INT33F0:00_CsiBayer", "0"},
303         {"INT33F0:00_CamClk", "1"},
304         {"gmin_V2P8GPIO", "402"},
305         {},
306 };
307
308
309 static const struct gmin_cfg_var i8880_vars[] = {
310         {"XXOV2680:00_CsiPort", "1"},
311         {"XXOV2680:00_CsiLanes", "1"},
312         {"XXOV2680:00_CamClk", "0"},
313         {"XXGC0310:00_CsiPort", "0"},
314         {"XXGC0310:00_CsiLanes", "1"},
315         {"XXGC0310:00_CamClk", "1"},
316         {},
317 };
318
319 static const struct {
320         const char *dmi_board_name;
321         const struct gmin_cfg_var *vars;
322 } hard_vars[] = {
323         { "BYT-T FFD8", ffrd8_vars },
324         { "T100TA", t100_vars },
325         { "MRD7", mrd7_vars },
326         { "ST70408", ecs7_vars },
327         { "VTA0803", i8880_vars },
328 };
329
330
331 #define GMIN_CFG_VAR_EFI_GUID EFI_GUID(0xecb54cd9, 0xe5ae, 0x4fdc, \
332                                        0xa9, 0x71, 0xe8, 0x77,     \
333                                        0x75, 0x60, 0x68, 0xf7)
334
335 #define CFG_VAR_NAME_MAX 64
336
337 static int gmin_platform_init(struct i2c_client *client)
338 {
339         return 0;
340 }
341
342 static int gmin_platform_deinit(void)
343 {
344         return 0;
345 }
346
347 static struct gmin_subdev *gmin_subdev_add(struct v4l2_subdev *subdev)
348 {
349         int i, ret;
350         struct device *dev;
351         struct i2c_client *client = v4l2_get_subdevdata(subdev);
352
353         if (!pmic_id)
354                 pmic_id = PMIC_REGULATOR;
355
356         if (!client)
357                 return NULL;
358
359         dev = &client->dev;
360
361         for (i = 0; i < MAX_SUBDEVS && gmin_subdevs[i].subdev; i++)
362                 ;
363         if (i >= MAX_SUBDEVS)
364                 return NULL;
365
366         dev_info(dev,
367                 "gmin: initializing atomisp module subdev data.PMIC ID %d\n",
368                 pmic_id);
369
370         gmin_subdevs[i].subdev = subdev;
371         gmin_subdevs[i].clock_num = gmin_get_var_int(dev, "CamClk", 0);
372         /*WA:CHT requires XTAL clock as PLL is not stable.*/
373         gmin_subdevs[i].clock_src = gmin_get_var_int(dev, "ClkSrc",
374                                                         VLV2_CLK_PLL_19P2MHZ);
375         gmin_subdevs[i].csi_port = gmin_get_var_int(dev, "CsiPort", 0);
376         gmin_subdevs[i].csi_lanes = gmin_get_var_int(dev, "CsiLanes", 1);
377         gmin_subdevs[i].gpio0 = gpiod_get_index(dev, NULL, 0, GPIOD_OUT_LOW);
378         gmin_subdevs[i].gpio1 = gpiod_get_index(dev, NULL, 1, GPIOD_OUT_LOW);
379
380         if (!IS_ERR(gmin_subdevs[i].gpio0)) {
381                 ret = gpiod_direction_output(gmin_subdevs[i].gpio0, 0);
382                 if (ret)
383                         dev_err(dev, "gpio0 set output failed: %d\n", ret);
384         } else {
385                 gmin_subdevs[i].gpio0 = NULL;
386         }
387
388         if (!IS_ERR(gmin_subdevs[i].gpio1)) {
389                 ret = gpiod_direction_output(gmin_subdevs[i].gpio1, 0);
390                 if (ret)
391                         dev_err(dev, "gpio1 set output failed: %d\n", ret);
392         } else {
393                 gmin_subdevs[i].gpio1 = NULL;
394         }
395
396         if (pmic_id == PMIC_REGULATOR) {
397                 gmin_subdevs[i].v1p8_reg = regulator_get(dev, "V1P8SX");
398                 gmin_subdevs[i].v2p8_reg = regulator_get(dev, "V2P8SX");
399                 gmin_subdevs[i].v1p2_reg = regulator_get(dev, "V1P2A");
400                 gmin_subdevs[i].v2p8_vcm_reg = regulator_get(dev, "VPROG4B");
401
402                 /* Note: ideally we would initialize v[12]p8_on to the
403                  * output of regulator_is_enabled(), but sadly that
404                  * API is broken with the current drivers, returning
405                  * "1" for a regulator that will then emit a
406                  * "unbalanced disable" WARNing if we try to disable
407                  * it.
408                  */
409         }
410
411         return &gmin_subdevs[i];
412 }
413
414 static struct gmin_subdev *find_gmin_subdev(struct v4l2_subdev *subdev)
415 {
416         int i;
417
418         for (i = 0; i < MAX_SUBDEVS; i++)
419                 if (gmin_subdevs[i].subdev == subdev)
420                         return &gmin_subdevs[i];
421         return gmin_subdev_add(subdev);
422 }
423
424 static int gmin_gpio0_ctrl(struct v4l2_subdev *subdev, int on)
425 {
426         struct gmin_subdev *gs = find_gmin_subdev(subdev);
427
428         if (gs && gs->gpio0) {
429                 gpiod_set_value(gs->gpio0, on);
430                 return 0;
431         }
432         return -EINVAL;
433 }
434
435 static int gmin_gpio1_ctrl(struct v4l2_subdev *subdev, int on)
436 {
437         struct gmin_subdev *gs = find_gmin_subdev(subdev);
438
439         if (gs && gs->gpio1) {
440                 gpiod_set_value(gs->gpio1, on);
441                 return 0;
442         }
443         return -EINVAL;
444 }
445
446 static int gmin_v1p2_ctrl(struct v4l2_subdev *subdev, int on)
447 {
448         struct gmin_subdev *gs = find_gmin_subdev(subdev);
449
450         if (gs && gs->v1p2_on == on)
451                 return 0;
452         gs->v1p2_on = on;
453
454         if (gs->v1p2_reg) {
455                 if (on)
456                         return regulator_enable(gs->v1p2_reg);
457                 else
458                         return regulator_disable(gs->v1p2_reg);
459         }
460
461         /*TODO:v1p2 needs to extend to other PMICs*/
462
463         return -EINVAL;
464 }
465
466 static int gmin_v1p8_ctrl(struct v4l2_subdev *subdev, int on)
467 {
468         struct gmin_subdev *gs = find_gmin_subdev(subdev);
469         int ret;
470
471         if (v1p8_gpio == V1P8_GPIO_UNSET) {
472                 v1p8_gpio = gmin_get_var_int(NULL, "V1P8GPIO", V1P8_GPIO_NONE);
473                 if (v1p8_gpio != V1P8_GPIO_NONE) {
474                         pr_info("atomisp_gmin_platform: 1.8v power on GPIO %d\n",
475                                 v1p8_gpio);
476                         ret = gpio_request(v1p8_gpio, "camera_v1p8_en");
477                         if (!ret)
478                                 ret = gpio_direction_output(v1p8_gpio, 0);
479                         if (ret)
480                                 pr_err("V1P8 GPIO initialization failed\n");
481                 }
482         }
483
484         if (gs && gs->v1p8_on == on)
485                 return 0;
486         gs->v1p8_on = on;
487
488         if (v1p8_gpio >= 0)
489                 gpio_set_value(v1p8_gpio, on);
490
491         if (gs->v1p8_reg) {
492                 regulator_set_voltage(gs->v1p8_reg, 1800000, 1800000);
493                 if (on)
494                         return regulator_enable(gs->v1p8_reg);
495                 else
496                         return regulator_disable(gs->v1p8_reg);
497         }
498
499         return -EINVAL;
500 }
501
502 static int gmin_v2p8_ctrl(struct v4l2_subdev *subdev, int on)
503 {
504         struct gmin_subdev *gs = find_gmin_subdev(subdev);
505         int ret;
506
507         if (v2p8_gpio == V2P8_GPIO_UNSET) {
508                 v2p8_gpio = gmin_get_var_int(NULL, "V2P8GPIO", V2P8_GPIO_NONE);
509                 if (v2p8_gpio != V2P8_GPIO_NONE) {
510                         pr_info("atomisp_gmin_platform: 2.8v power on GPIO %d\n",
511                                 v2p8_gpio);
512                         ret = gpio_request(v2p8_gpio, "camera_v2p8");
513                         if (!ret)
514                                 ret = gpio_direction_output(v2p8_gpio, 0);
515                         if (ret)
516                                 pr_err("V2P8 GPIO initialization failed\n");
517                 }
518         }
519
520         if (gs && gs->v2p8_on == on)
521                 return 0;
522         gs->v2p8_on = on;
523
524         if (v2p8_gpio >= 0)
525                 gpio_set_value(v2p8_gpio, on);
526
527         if (gs->v2p8_reg) {
528                 regulator_set_voltage(gs->v2p8_reg, 2900000, 2900000);
529                 if (on)
530                         return regulator_enable(gs->v2p8_reg);
531                 else
532                         return regulator_disable(gs->v2p8_reg);
533         }
534
535         return -EINVAL;
536 }
537
538 static int gmin_flisclk_ctrl(struct v4l2_subdev *subdev, int on)
539 {
540         int ret = 0;
541         struct gmin_subdev *gs = find_gmin_subdev(subdev);
542
543         if (on)
544                 ret = vlv2_plat_set_clock_freq(gs->clock_num, gs->clock_src);
545         if (ret)
546                 return ret;
547         return vlv2_plat_configure_clock(gs->clock_num,
548                                          on ? VLV2_CLK_ON : VLV2_CLK_OFF);
549 }
550
551 static int gmin_csi_cfg(struct v4l2_subdev *sd, int flag)
552 {
553         struct i2c_client *client = v4l2_get_subdevdata(sd);
554         struct gmin_subdev *gs = find_gmin_subdev(sd);
555
556         if (!client || !gs)
557                 return -ENODEV;
558
559         return camera_sensor_csi(sd, gs->csi_port, gs->csi_lanes,
560                                  gs->csi_fmt, gs->csi_bayer, flag);
561 }
562
563 static struct camera_vcm_control *gmin_get_vcm_ctrl(struct v4l2_subdev *subdev,
564                                                 char *camera_module)
565 {
566         struct i2c_client *client = v4l2_get_subdevdata(subdev);
567         struct gmin_subdev *gs = find_gmin_subdev(subdev);
568         struct camera_vcm_control *vcm;
569
570         if (client == NULL || gs == NULL)
571                 return NULL;
572
573         if (!camera_module)
574                 return NULL;
575
576         mutex_lock(&vcm_lock);
577         list_for_each_entry(vcm, &vcm_devices, list) {
578                 if (!strcmp(camera_module, vcm->camera_module)) {
579                         mutex_unlock(&vcm_lock);
580                         return vcm;
581                 }
582         }
583
584         mutex_unlock(&vcm_lock);
585         return NULL;
586 }
587
588 static struct camera_sensor_platform_data gmin_plat = {
589         .gpio0_ctrl = gmin_gpio0_ctrl,
590         .gpio1_ctrl = gmin_gpio1_ctrl,
591         .v1p8_ctrl = gmin_v1p8_ctrl,
592         .v2p8_ctrl = gmin_v2p8_ctrl,
593         .v1p2_ctrl = gmin_v1p2_ctrl,
594         .flisclk_ctrl = gmin_flisclk_ctrl,
595         .platform_init = gmin_platform_init,
596         .platform_deinit = gmin_platform_deinit,
597         .csi_cfg = gmin_csi_cfg,
598         .get_vcm_ctrl = gmin_get_vcm_ctrl,
599 };
600
601 struct camera_sensor_platform_data *gmin_camera_platform_data(
602                 struct v4l2_subdev *subdev,
603                 enum atomisp_input_format csi_format,
604                 enum atomisp_bayer_order csi_bayer)
605 {
606         struct gmin_subdev *gs = find_gmin_subdev(subdev);
607
608         gs->csi_fmt = csi_format;
609         gs->csi_bayer = csi_bayer;
610
611         return &gmin_plat;
612 }
613 EXPORT_SYMBOL_GPL(gmin_camera_platform_data);
614
615 int atomisp_gmin_register_vcm_control(struct camera_vcm_control *vcmCtrl)
616 {
617         if (!vcmCtrl)
618                 return -EINVAL;
619
620         mutex_lock(&vcm_lock);
621         list_add_tail(&vcmCtrl->list, &vcm_devices);
622         mutex_unlock(&vcm_lock);
623
624         return 0;
625 }
626 EXPORT_SYMBOL_GPL(atomisp_gmin_register_vcm_control);
627
628 /* Retrieves a device-specific configuration variable.  The dev
629  * argument should be a device with an ACPI companion, as all
630  * configuration is based on firmware ID.
631  */
632 int gmin_get_config_var(struct device *dev, const char *var, char *out,
633                         size_t *out_len)
634 {
635         char var8[CFG_VAR_NAME_MAX];
636         efi_char16_t var16[CFG_VAR_NAME_MAX];
637         struct efivar_entry *ev;
638         int i, j, ret;
639
640         if (dev && ACPI_COMPANION(dev))
641                 dev = &ACPI_COMPANION(dev)->dev;
642
643         if (dev)
644                 ret = snprintf(var8, sizeof(var8), "%s_%s", dev_name(dev), var);
645         else
646                 ret = snprintf(var8, sizeof(var8), "gmin_%s", var);
647
648         if (ret < 0 || ret >= sizeof(var8) - 1)
649                 return -EINVAL;
650
651         /* First check a hard-coded list of board-specific variables.
652          * Some device firmwares lack the ability to set EFI variables at
653          * runtime.
654          */
655         for (i = 0; i < ARRAY_SIZE(hard_vars); i++) {
656                 if (dmi_match(DMI_BOARD_NAME, hard_vars[i].dmi_board_name)) {
657                         for (j = 0; hard_vars[i].vars[j].name; j++) {
658                                 size_t vl;
659                                 const struct gmin_cfg_var *gv;
660
661                                 gv = &hard_vars[i].vars[j];
662                                 vl = strlen(gv->val);
663
664                                 if (strcmp(var8, gv->name))
665                                         continue;
666                                 if (vl > *out_len - 1)
667                                         return -ENOSPC;
668
669                                 memcpy(out, gv->val, min(*out_len, vl+1));
670                                 out[*out_len-1] = 0;
671                                 *out_len = vl;
672
673                                 return 0;
674                         }
675                 }
676         }
677
678         /* Our variable names are ASCII by construction, but EFI names
679          * are wide chars.  Convert and zero-pad.
680          */
681         memset(var16, 0, sizeof(var16));
682         for (i = 0; i < sizeof(var8) && var8[i]; i++)
683                 var16[i] = var8[i];
684
685         /* To avoid owerflows when calling the efivar API */
686         if (*out_len > ULONG_MAX)
687                 return -EINVAL;
688
689         /* Not sure this API usage is kosher; efivar_entry_get()'s
690          * implementation simply uses VariableName and VendorGuid from
691          * the struct and ignores the rest, but it seems like there
692          * ought to be an "official" efivar_entry registered
693          * somewhere?
694          */
695         ev = kzalloc(sizeof(*ev), GFP_KERNEL);
696         if (!ev)
697                 return -ENOMEM;
698         memcpy(&ev->var.VariableName, var16, sizeof(var16));
699         ev->var.VendorGuid = GMIN_CFG_VAR_EFI_GUID;
700         ev->var.DataSize = *out_len;
701
702         ret = efivar_entry_get(ev, &ev->var.Attributes,
703                                &ev->var.DataSize, ev->var.Data);
704         if (ret == 0) {
705                 memcpy(out, ev->var.Data, ev->var.DataSize);
706                 *out_len = ev->var.DataSize;
707         } else if (dev) {
708                 dev_warn(dev, "Failed to find gmin variable %s\n", var8);
709         }
710
711         kfree(ev);
712
713         return ret;
714 }
715 EXPORT_SYMBOL_GPL(gmin_get_config_var);
716
717 int gmin_get_var_int(struct device *dev, const char *var, int def)
718 {
719         char val[CFG_VAR_NAME_MAX];
720         size_t len = sizeof(val);
721         long result;
722         int ret;
723
724         ret = gmin_get_config_var(dev, var, val, &len);
725         if (!ret) {
726                 val[len] = 0;
727                 ret = kstrtol(val, 0, &result);
728         }
729
730         return ret ? def : result;
731 }
732 EXPORT_SYMBOL_GPL(gmin_get_var_int);
733
734 int camera_sensor_csi(struct v4l2_subdev *sd, u32 port,
735                       u32 lanes, u32 format, u32 bayer_order, int flag)
736 {
737         struct i2c_client *client = v4l2_get_subdevdata(sd);
738         struct camera_mipi_info *csi = NULL;
739
740         if (flag) {
741                 csi = kzalloc(sizeof(*csi), GFP_KERNEL);
742                 if (!csi) {
743                         dev_err(&client->dev, "out of memory\n");
744                         return -ENOMEM;
745                 }
746                 csi->port = port;
747                 csi->num_lanes = lanes;
748                 csi->input_format = format;
749                 csi->raw_bayer_order = bayer_order;
750                 v4l2_set_subdev_hostdata(sd, (void *)csi);
751                 csi->metadata_format = ATOMISP_INPUT_FORMAT_EMBEDDED;
752                 csi->metadata_effective_width = NULL;
753                 dev_info(&client->dev,
754                          "camera pdata: port: %d lanes: %d order: %8.8x\n",
755                          port, lanes, bayer_order);
756         } else {
757                 csi = v4l2_get_subdev_hostdata(sd);
758                 kfree(csi);
759         }
760
761         return 0;
762 }
763 EXPORT_SYMBOL_GPL(camera_sensor_csi);
764
765 /* PCI quirk: The BYT ISP advertises PCI runtime PM but it doesn't
766  * work.  Disable so the kernel framework doesn't hang the device
767  * trying.  The driver itself does direct calls to the PUNIT to manage
768  * ISP power.
769  */
770 static void isp_pm_cap_fixup(struct pci_dev *dev)
771 {
772         dev_info(&dev->dev, "Disabling PCI power management on camera ISP\n");
773         dev->pm_cap = 0;
774 }
775 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x0f38, isp_pm_cap_fixup);