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