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 /* Valid clock number range from 0 to 5 */
30 #define MAX_CLK_COUNT 5
31
32 /* X-Powers AXP288 register set */
33 #define ALDO1_SEL_REG 0x28
34 #define ALDO1_CTRL3_REG 0x13
35 #define ALDO1_2P8V 0x16
36 #define ALDO1_CTRL3_SHIFT 0x05
37
38 #define ELDO_CTRL_REG 0x12
39
40 #define ELDO1_SEL_REG 0x19
41 #define ELDO1_1P6V 0x12
42 #define ELDO1_CTRL_SHIFT 0x00
43
44 #define ELDO2_SEL_REG 0x1a
45 #define ELDO2_1P8V 0x16
46 #define ELDO2_CTRL_SHIFT 0x01
47
48 /* TI SND9039 PMIC register set */
49 #define LDO9_REG 0x49
50 #define LDO10_REG 0x4a
51 #define LDO11_REG 0x4b
52
53 #define LDO_2P8V_ON 0x2f /* 0x2e selects 2.85V ... */
54 #define LDO_2P8V_OFF 0x2e /* ... bottom bit is "enabled" */
55
56 #define LDO_1P8V_ON 0x59 /* 0x58 selects 1.80V ... */
57 #define LDO_1P8V_OFF 0x58 /* ... bottom bit is "enabled" */
58
59 /* CRYSTAL COVE PMIC register set */
60 #define CRYSTAL_BYT_1P8V_REG 0x5d
61 #define CRYSTAL_BYT_2P8V_REG 0x66
62
63 #define CRYSTAL_CHT_1P8V_REG 0x57
64 #define CRYSTAL_CHT_2P8V_REG 0x5d
65
66 #define CRYSTAL_ON 0x63
67 #define CRYSTAL_OFF 0x62
68
69 struct gmin_subdev {
70 struct v4l2_subdev *subdev;
71 enum clock_rate clock_src;
72 struct clk *pmc_clk;
73 struct gpio_desc *gpio0;
74 struct gpio_desc *gpio1;
75 struct regulator *v1p8_reg;
76 struct regulator *v2p8_reg;
77 struct regulator *v1p2_reg;
78 enum atomisp_camera_port csi_port;
79 unsigned int csi_lanes;
80 enum atomisp_input_format csi_fmt;
81 enum atomisp_bayer_order csi_bayer;
82
83 bool clock_on;
84 bool v1p8_on;
85 bool v2p8_on;
86 bool v1p2_on;
87
88 int v1p8_gpio;
89 int v2p8_gpio;
90
91 u8 pwm_i2c_addr;
92
93 /* For PMIC AXP */
94 int eldo1_sel_reg, eldo1_1p6v, eldo1_ctrl_shift;
95 int eldo2_sel_reg, eldo2_1p8v, eldo2_ctrl_shift;
96 };
97
98 static struct gmin_subdev gmin_subdevs[MAX_SUBDEVS];
99
100 /* ACPI HIDs for the PMICs that could be used by this driver */
101 #define PMIC_ACPI_AXP "INT33F4" /* XPower AXP288 PMIC */
102 #define PMIC_ACPI_TI "INT33F5" /* Dollar Cove TI PMIC */
103 #define PMIC_ACPI_CRYSTALCOVE "INT33FD" /* Crystal Cove PMIC */
104
105 #define PMIC_PLATFORM_TI "intel_soc_pmic_chtdc_ti"
106
107 static enum {
108 PMIC_UNSET = 0,
109 PMIC_REGULATOR,
110 PMIC_AXP,
111 PMIC_TI,
112 PMIC_CRYSTALCOVE
113 } pmic_id;
114
115 static const char *pmic_name[] = {
116 [PMIC_UNSET] = "ACPI device PM",
117 [PMIC_REGULATOR] = "regulator driver",
118 [PMIC_AXP] = "XPower AXP288 PMIC",
119 [PMIC_TI] = "Dollar Cove TI PMIC",
120 [PMIC_CRYSTALCOVE] = "Crystal Cove PMIC",
121 };
122
123 static DEFINE_MUTEX(gmin_regulator_mutex);
124 static int gmin_v1p8_enable_count;
125 static int gmin_v2p8_enable_count;
126
127 /* The atomisp uses subdev==NULL for the end-of-list marker, so leave space. */
128 static struct intel_v4l2_subdev_table pdata_subdevs[MAX_SUBDEVS + 1];
129
130 static struct gmin_subdev *find_gmin_subdev(struct v4l2_subdev *subdev);
131
atomisp_platform_get_subdevs(void)132 const struct intel_v4l2_subdev_table *atomisp_platform_get_subdevs(void)
133 {
134 return pdata_subdevs;
135 }
136 EXPORT_SYMBOL_GPL(atomisp_platform_get_subdevs);
137
atomisp_register_i2c_module(struct v4l2_subdev * subdev,struct camera_sensor_platform_data * plat_data)138 int atomisp_register_i2c_module(struct v4l2_subdev *subdev,
139 struct camera_sensor_platform_data *plat_data)
140 {
141 int i;
142 struct gmin_subdev *gs;
143 struct i2c_client *client = v4l2_get_subdevdata(subdev);
144 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
145
146 /* The windows driver model (and thus most BIOSes by default)
147 * uses ACPI runtime power management for camera devices, but
148 * we don't. Disable it, or else the rails will be needlessly
149 * tickled during suspend/resume. This has caused power and
150 * performance issues on multiple devices.
151 */
152
153 /*
154 * Turn off the device before disabling ACPI power resources
155 * (the sensor driver has already probed it at this point).
156 * This avoids leaking the reference count of the (possibly shared)
157 * ACPI power resources which were enabled/referenced before probe().
158 */
159 acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
160 adev->power.flags.power_resources = 0;
161
162 for (i = 0; i < MAX_SUBDEVS; i++)
163 if (!pdata_subdevs[i].subdev)
164 break;
165
166 if (i == MAX_SUBDEVS)
167 return -ENOMEM;
168
169 /* Note subtlety of initialization order: at the point where
170 * this registration API gets called, the platform data
171 * callbacks have probably already been invoked, so the
172 * gmin_subdev struct is already initialized for us.
173 */
174 gs = find_gmin_subdev(subdev);
175 if (!gs)
176 return -ENODEV;
177
178 pdata_subdevs[i].port = gs->csi_port;
179 pdata_subdevs[i].lanes = gs->csi_lanes;
180 pdata_subdevs[i].subdev = subdev;
181 return 0;
182 }
183 EXPORT_SYMBOL_GPL(atomisp_register_i2c_module);
184
atomisp_gmin_remove_subdev(struct v4l2_subdev * sd)185 int atomisp_gmin_remove_subdev(struct v4l2_subdev *sd)
186 {
187 int i, j;
188
189 if (!sd)
190 return 0;
191
192 for (i = 0; i < MAX_SUBDEVS; i++) {
193 if (pdata_subdevs[i].subdev == sd) {
194 for (j = i + 1; j <= MAX_SUBDEVS; j++)
195 pdata_subdevs[j - 1] = pdata_subdevs[j];
196 }
197 if (gmin_subdevs[i].subdev == sd) {
198 if (gmin_subdevs[i].gpio0)
199 gpiod_put(gmin_subdevs[i].gpio0);
200 gmin_subdevs[i].gpio0 = NULL;
201 if (gmin_subdevs[i].gpio1)
202 gpiod_put(gmin_subdevs[i].gpio1);
203 gmin_subdevs[i].gpio1 = NULL;
204 if (pmic_id == PMIC_REGULATOR) {
205 regulator_put(gmin_subdevs[i].v1p8_reg);
206 regulator_put(gmin_subdevs[i].v2p8_reg);
207 regulator_put(gmin_subdevs[i].v1p2_reg);
208 }
209 gmin_subdevs[i].subdev = NULL;
210 }
211 }
212 return 0;
213 }
214 EXPORT_SYMBOL_GPL(atomisp_gmin_remove_subdev);
215
216 struct gmin_cfg_var {
217 const char *name, *val;
218 };
219
220 static struct gmin_cfg_var ffrd8_vars[] = {
221 { "INTCF1B:00_ImxId", "0x134" },
222 { "INTCF1B:00_CsiPort", "1" },
223 { "INTCF1B:00_CsiLanes", "4" },
224 { "INTCF1B:00_CamClk", "0" },
225 {},
226 };
227
228 /* Cribbed from MCG defaults in the mt9m114 driver, not actually verified
229 * vs. T100 hardware
230 */
231 static struct gmin_cfg_var t100_vars[] = {
232 { "INT33F0:00_CsiPort", "0" },
233 { "INT33F0:00_CsiLanes", "1" },
234 { "INT33F0:00_CamClk", "1" },
235 {},
236 };
237
238 static struct gmin_cfg_var mrd7_vars[] = {
239 {"INT33F8:00_CamType", "1"},
240 {"INT33F8:00_CsiPort", "1"},
241 {"INT33F8:00_CsiLanes", "2"},
242 {"INT33F8:00_CsiFmt", "13"},
243 {"INT33F8:00_CsiBayer", "0"},
244 {"INT33F8:00_CamClk", "0"},
245
246 {"INT33F9:00_CamType", "1"},
247 {"INT33F9:00_CsiPort", "0"},
248 {"INT33F9:00_CsiLanes", "1"},
249 {"INT33F9:00_CsiFmt", "13"},
250 {"INT33F9:00_CsiBayer", "0"},
251 {"INT33F9:00_CamClk", "1"},
252 {},
253 };
254
255 static struct gmin_cfg_var ecs7_vars[] = {
256 {"INT33BE:00_CsiPort", "1"},
257 {"INT33BE:00_CsiLanes", "2"},
258 {"INT33BE:00_CsiFmt", "13"},
259 {"INT33BE:00_CsiBayer", "2"},
260 {"INT33BE:00_CamClk", "0"},
261
262 {"INT33F0:00_CsiPort", "0"},
263 {"INT33F0:00_CsiLanes", "1"},
264 {"INT33F0:00_CsiFmt", "13"},
265 {"INT33F0:00_CsiBayer", "0"},
266 {"INT33F0:00_CamClk", "1"},
267 {"gmin_V2P8GPIO", "402"},
268 {},
269 };
270
271 static struct gmin_cfg_var i8880_vars[] = {
272 {"XXOV2680:00_CsiPort", "1"},
273 {"XXOV2680:00_CsiLanes", "1"},
274 {"XXOV2680:00_CamClk", "0"},
275
276 {"XXGC0310:00_CsiPort", "0"},
277 {"XXGC0310:00_CsiLanes", "1"},
278 {"XXGC0310:00_CamClk", "1"},
279 {},
280 };
281
282 /*
283 * Surface 3 does not describe CsiPort/CsiLanes in both DSDT and EFI.
284 */
285 static struct gmin_cfg_var surface3_vars[] = {
286 {"APTA0330:00_CsiPort", "0"},
287 {"APTA0330:00_CsiLanes", "2"},
288
289 {"OVTI8835:00_CsiPort", "1"},
290 {"OVTI8835:00_CsiLanes", "4"},
291 {},
292 };
293
294 static struct gmin_cfg_var lenovo_ideapad_miix_310_vars[] = {
295 /* _DSM contains the wrong CsiPort! */
296 { "OVTI2680:01_CsiPort", "0" },
297 {}
298 };
299
300 static const struct dmi_system_id gmin_vars[] = {
301 /*
302 * These DMI IDs were present when the atomisp driver was merged into
303 * drivers/staging and it is unclear if they are really necessary.
304 */
305 {
306 .ident = "BYT-T FFD8",
307 .matches = {
308 DMI_MATCH(DMI_BOARD_NAME, "BYT-T FFD8"),
309 },
310 .driver_data = ffrd8_vars,
311 },
312 {
313 .ident = "T100TA",
314 .matches = {
315 DMI_MATCH(DMI_BOARD_NAME, "T100TA"),
316 },
317 .driver_data = t100_vars,
318 },
319 {
320 .ident = "MRD7",
321 .matches = {
322 DMI_MATCH(DMI_BOARD_NAME, "TABLET"),
323 DMI_MATCH(DMI_BOARD_VERSION, "MRD 7"),
324 },
325 .driver_data = mrd7_vars,
326 },
327 {
328 .ident = "ST70408",
329 .matches = {
330 DMI_MATCH(DMI_BOARD_NAME, "ST70408"),
331 },
332 .driver_data = ecs7_vars,
333 },
334 {
335 .ident = "VTA0803",
336 .matches = {
337 DMI_MATCH(DMI_BOARD_NAME, "VTA0803"),
338 },
339 .driver_data = i8880_vars,
340 },
341 /* Later added DMI ids, these are confirmed to really be necessary! */
342 {
343 .ident = "Surface 3",
344 .matches = {
345 DMI_MATCH(DMI_BOARD_NAME, "Surface 3"),
346 },
347 .driver_data = surface3_vars,
348 },
349 {
350 .ident = "Lenovo Ideapad Miix 310",
351 .matches = {
352 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
353 DMI_MATCH(DMI_PRODUCT_VERSION, "MIIX 310-10"),
354 },
355 .driver_data = lenovo_ideapad_miix_310_vars,
356 },
357 {}
358 };
359
360 #define GMIN_CFG_VAR_EFI_GUID EFI_GUID(0xecb54cd9, 0xe5ae, 0x4fdc, \
361 0xa9, 0x71, 0xe8, 0x77, \
362 0x75, 0x60, 0x68, 0xf7)
363
364 static const guid_t atomisp_dsm_guid = GUID_INIT(0xdc2f6c4f, 0x045b, 0x4f1d,
365 0x97, 0xb9, 0x88, 0x2a,
366 0x68, 0x60, 0xa4, 0xbe);
367
368 #define CFG_VAR_NAME_MAX 64
369
370 #define GMIN_PMC_CLK_NAME 14 /* "pmc_plt_clk_[0..5]" */
371 static char gmin_pmc_clk_name[GMIN_PMC_CLK_NAME];
372
gmin_i2c_dev_exists(struct device * dev,char * name,struct i2c_client ** client)373 static struct i2c_client *gmin_i2c_dev_exists(struct device *dev, char *name,
374 struct i2c_client **client)
375 {
376 struct acpi_device *adev;
377
378 adev = acpi_dev_get_first_match_dev(name, NULL, -1);
379 if (!adev)
380 return NULL;
381
382 *client = i2c_find_device_by_fwnode(acpi_fwnode_handle(adev));
383 acpi_dev_put(adev);
384 if (!*client)
385 return NULL;
386
387 dev_dbg(dev, "found '%s' at address 0x%02x, adapter %d\n",
388 (*client)->name, (*client)->addr, (*client)->adapter->nr);
389 return *client;
390 }
391
gmin_i2c_write(struct device * dev,u16 i2c_addr,u8 reg,u32 value,u32 mask)392 static int gmin_i2c_write(struct device *dev, u16 i2c_addr, u8 reg,
393 u32 value, u32 mask)
394 {
395 int ret;
396
397 /*
398 * FIXME: Right now, the intel_pmic driver just write values
399 * directly at the regmap, instead of properly implementing
400 * i2c_transfer() mechanism. Let's use the same interface here,
401 * as otherwise we may face issues.
402 */
403
404 dev_dbg(dev,
405 "I2C write, addr: 0x%02x, reg: 0x%02x, value: 0x%02x, mask: 0x%02x\n",
406 i2c_addr, reg, value, mask);
407
408 ret = intel_soc_pmic_exec_mipi_pmic_seq_element(i2c_addr, reg, value, mask);
409 if (ret == -EOPNOTSUPP)
410 dev_err(dev,
411 "ACPI didn't mapped the OpRegion needed to access I2C address 0x%02x.\n"
412 "Need to compile the kernel using CONFIG_*_PMIC_OPREGION settings\n",
413 i2c_addr);
414
415 return ret;
416 }
417
atomisp_get_acpi_power(struct device * dev)418 static int atomisp_get_acpi_power(struct device *dev)
419 {
420 char name[5];
421 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
422 struct acpi_buffer b_name = { sizeof(name), name };
423 union acpi_object *package, *element;
424 acpi_handle handle = ACPI_HANDLE(dev);
425 acpi_handle rhandle;
426 acpi_status status;
427 int clock_num = -1;
428 int i;
429
430 status = acpi_evaluate_object(handle, "_PR0", NULL, &buffer);
431 if (!ACPI_SUCCESS(status))
432 return -1;
433
434 package = buffer.pointer;
435
436 if (!buffer.length || !package
437 || package->type != ACPI_TYPE_PACKAGE
438 || !package->package.count)
439 goto fail;
440
441 for (i = 0; i < package->package.count; i++) {
442 element = &package->package.elements[i];
443
444 if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
445 continue;
446
447 rhandle = element->reference.handle;
448 if (!rhandle)
449 goto fail;
450
451 acpi_get_name(rhandle, ACPI_SINGLE_NAME, &b_name);
452
453 dev_dbg(dev, "Found PM resource '%s'\n", name);
454 if (strlen(name) == 4 && !strncmp(name, "CLK", 3)) {
455 if (name[3] >= '0' && name[3] <= '4')
456 clock_num = name[3] - '0';
457 #if 0
458 /*
459 * We could abort here, but let's parse all resources,
460 * as this is helpful for debugging purposes
461 */
462 if (clock_num >= 0)
463 break;
464 #endif
465 }
466 }
467
468 fail:
469 ACPI_FREE(buffer.pointer);
470
471 return clock_num;
472 }
473
gmin_get_pmic_id_and_addr(struct device * dev)474 static u8 gmin_get_pmic_id_and_addr(struct device *dev)
475 {
476 struct i2c_client *power = NULL;
477 static u8 pmic_i2c_addr;
478
479 if (pmic_id)
480 return pmic_i2c_addr;
481
482 if (gmin_i2c_dev_exists(dev, PMIC_ACPI_TI, &power)) {
483 pmic_id = PMIC_TI;
484 } else if (gmin_i2c_dev_exists(dev, PMIC_ACPI_AXP, &power)) {
485 pmic_id = PMIC_AXP;
486 } else if (gmin_i2c_dev_exists(dev, PMIC_ACPI_CRYSTALCOVE, &power)) {
487 pmic_id = PMIC_CRYSTALCOVE;
488 } else {
489 pmic_id = PMIC_REGULATOR;
490 return 0;
491 }
492
493 pmic_i2c_addr = power->addr;
494 put_device(&power->dev);
495 return pmic_i2c_addr;
496 }
497
gmin_detect_pmic(struct v4l2_subdev * subdev)498 static int gmin_detect_pmic(struct v4l2_subdev *subdev)
499 {
500 struct i2c_client *client = v4l2_get_subdevdata(subdev);
501 struct device *dev = &client->dev;
502 u8 pmic_i2c_addr;
503
504 pmic_i2c_addr = gmin_get_pmic_id_and_addr(dev);
505 dev_info(dev, "gmin: power management provided via %s (i2c addr 0x%02x)\n",
506 pmic_name[pmic_id], pmic_i2c_addr);
507 return pmic_i2c_addr;
508 }
509
gmin_subdev_add(struct gmin_subdev * gs)510 static int gmin_subdev_add(struct gmin_subdev *gs)
511 {
512 struct i2c_client *client = v4l2_get_subdevdata(gs->subdev);
513 struct device *dev = &client->dev;
514 struct acpi_device *adev = ACPI_COMPANION(dev);
515 int ret, default_val, clock_num = -1;
516
517 dev_info(dev, "%s: ACPI path is %pfw\n", __func__, dev_fwnode(dev));
518
519 /*WA:CHT requires XTAL clock as PLL is not stable.*/
520 gs->clock_src = gmin_get_var_int(dev, false, "ClkSrc",
521 VLV2_CLK_PLL_19P2MHZ);
522
523 /*
524 * Get ACPI _PR0 derived clock here already because it is used
525 * to determine the csi_port default.
526 */
527 if (acpi_device_power_manageable(adev))
528 clock_num = atomisp_get_acpi_power(dev);
529
530 /* Compare clock to CsiPort 1 pmc-clock used in the CHT/BYT reference designs */
531 if (IS_ISP2401)
532 default_val = clock_num == 4 ? 1 : 0;
533 else
534 default_val = clock_num == 0 ? 1 : 0;
535
536 gs->csi_port = gmin_get_var_int(dev, false, "CsiPort", default_val);
537 gs->csi_lanes = gmin_get_var_int(dev, false, "CsiLanes", 1);
538
539 gs->gpio0 = gpiod_get_index(dev, NULL, 0, GPIOD_OUT_LOW);
540 if (IS_ERR(gs->gpio0))
541 gs->gpio0 = NULL;
542 else
543 dev_info(dev, "will handle gpio0 via ACPI\n");
544
545 gs->gpio1 = gpiod_get_index(dev, NULL, 1, GPIOD_OUT_LOW);
546 if (IS_ERR(gs->gpio1))
547 gs->gpio1 = NULL;
548 else
549 dev_info(dev, "will handle gpio1 via ACPI\n");
550
551 /*
552 * Those are used only when there is an external regulator apart
553 * from the PMIC that would be providing power supply, like on the
554 * two cases below:
555 *
556 * The ECS E7 board drives camera 2.8v from an external regulator
557 * instead of the PMIC. There's a gmin_CamV2P8 config variable
558 * that specifies the GPIO to handle this particular case,
559 * but this needs a broader architecture for handling camera power.
560 *
561 * The CHT RVP board drives camera 1.8v from an* external regulator
562 * instead of the PMIC just like ECS E7 board.
563 */
564
565 gs->v1p8_gpio = gmin_get_var_int(dev, true, "V1P8GPIO", -1);
566 gs->v2p8_gpio = gmin_get_var_int(dev, true, "V2P8GPIO", -1);
567
568 /*
569 * FIXME:
570 *
571 * The ACPI handling code checks for the _PR? tables in order to
572 * know what is required to switch the device from power state
573 * D0 (_PR0) up to D3COLD (_PR3).
574 *
575 * The adev->flags.power_manageable is set to true if the device
576 * has a _PR0 table, which can be checked by calling
577 * acpi_device_power_manageable(adev).
578 *
579 * However, this only says that the device can be set to power off
580 * mode.
581 *
582 * At least on the DSDT tables we've seen so far, there's no _PR3,
583 * nor _PS3 (which would have a somewhat similar effect).
584 * So, using ACPI for power management won't work, except if adding
585 * an ACPI override logic somewhere.
586 *
587 * So, at least for the existing devices we know, the check below
588 * will always be false.
589 */
590 if (acpi_device_can_wakeup(adev) &&
591 acpi_device_can_poweroff(adev)) {
592 dev_info(dev,
593 "gmin: power management provided via device PM\n");
594 return 0;
595 }
596
597 /*
598 * The code below is here due to backward compatibility with devices
599 * whose ACPI BIOS may not contain everything that would be needed
600 * in order to set clocks and do power management.
601 */
602
603 /*
604 * According with :
605 * https://github.com/projectceladon/hardware-intel-kernelflinger/blob/master/doc/fastboot.md
606 *
607 * The "CamClk" EFI var is set via fastboot on some Android devices,
608 * and seems to contain the number of the clock used to feed the
609 * sensor.
610 *
611 * On systems with a proper ACPI table, this is given via the _PR0
612 * power resource table. The logic below should first check if there
613 * is a power resource already, falling back to the EFI vars detection
614 * otherwise.
615 */
616
617 /* If getting the clock from _PR0 above failed, fall-back to EFI and/or DMI match */
618 if (clock_num < 0)
619 clock_num = gmin_get_var_int(dev, false, "CamClk", 0);
620
621 if (clock_num < 0 || clock_num > MAX_CLK_COUNT) {
622 dev_err(dev, "Invalid clock number\n");
623 return -EINVAL;
624 }
625
626 snprintf(gmin_pmc_clk_name, sizeof(gmin_pmc_clk_name),
627 "%s_%d", "pmc_plt_clk", clock_num);
628
629 gs->pmc_clk = devm_clk_get(dev, gmin_pmc_clk_name);
630 if (IS_ERR(gs->pmc_clk)) {
631 ret = PTR_ERR(gs->pmc_clk);
632 dev_err(dev, "Failed to get clk from %s: %d\n", gmin_pmc_clk_name, ret);
633 return ret;
634 }
635 dev_info(dev, "Will use CLK%d (%s)\n", clock_num, gmin_pmc_clk_name);
636
637 /*
638 * The firmware might enable the clock at
639 * boot (this information may or may not
640 * be reflected in the enable clock register).
641 * To change the rate we must disable the clock
642 * first to cover these cases. Due to common
643 * clock framework restrictions that do not allow
644 * to disable a clock that has not been enabled,
645 * we need to enable the clock first.
646 */
647 ret = clk_prepare_enable(gs->pmc_clk);
648 if (!ret)
649 clk_disable_unprepare(gs->pmc_clk);
650
651 switch (pmic_id) {
652 case PMIC_REGULATOR:
653 gs->v1p8_reg = regulator_get(dev, "V1P8SX");
654 gs->v2p8_reg = regulator_get(dev, "V2P8SX");
655
656 gs->v1p2_reg = regulator_get(dev, "V1P2A");
657
658 /* Note: ideally we would initialize v[12]p8_on to the
659 * output of regulator_is_enabled(), but sadly that
660 * API is broken with the current drivers, returning
661 * "1" for a regulator that will then emit a
662 * "unbalanced disable" WARNing if we try to disable
663 * it.
664 */
665 break;
666
667 case PMIC_AXP:
668 gs->eldo1_1p6v = gmin_get_var_int(dev, false,
669 "eldo1_1p8v",
670 ELDO1_1P6V);
671 gs->eldo1_sel_reg = gmin_get_var_int(dev, false,
672 "eldo1_sel_reg",
673 ELDO1_SEL_REG);
674 gs->eldo1_ctrl_shift = gmin_get_var_int(dev, false,
675 "eldo1_ctrl_shift",
676 ELDO1_CTRL_SHIFT);
677 gs->eldo2_1p8v = gmin_get_var_int(dev, false,
678 "eldo2_1p8v",
679 ELDO2_1P8V);
680 gs->eldo2_sel_reg = gmin_get_var_int(dev, false,
681 "eldo2_sel_reg",
682 ELDO2_SEL_REG);
683 gs->eldo2_ctrl_shift = gmin_get_var_int(dev, false,
684 "eldo2_ctrl_shift",
685 ELDO2_CTRL_SHIFT);
686 break;
687
688 default:
689 break;
690 }
691
692 return 0;
693 }
694
find_gmin_subdev(struct v4l2_subdev * subdev)695 static struct gmin_subdev *find_gmin_subdev(struct v4l2_subdev *subdev)
696 {
697 int i;
698
699 for (i = 0; i < MAX_SUBDEVS; i++)
700 if (gmin_subdevs[i].subdev == subdev)
701 return &gmin_subdevs[i];
702 return NULL;
703 }
704
find_free_gmin_subdev_slot(void)705 static struct gmin_subdev *find_free_gmin_subdev_slot(void)
706 {
707 unsigned int i;
708
709 for (i = 0; i < MAX_SUBDEVS; i++)
710 if (gmin_subdevs[i].subdev == NULL)
711 return &gmin_subdevs[i];
712 return NULL;
713 }
714
axp_regulator_set(struct device * dev,struct gmin_subdev * gs,int sel_reg,u8 setting,int ctrl_reg,int shift,bool on)715 static int axp_regulator_set(struct device *dev, struct gmin_subdev *gs,
716 int sel_reg, u8 setting,
717 int ctrl_reg, int shift, bool on)
718 {
719 int ret;
720 int val;
721
722 ret = gmin_i2c_write(dev, gs->pwm_i2c_addr, sel_reg, setting, 0xff);
723 if (ret)
724 return ret;
725
726 val = on ? 1 << shift : 0;
727
728 ret = gmin_i2c_write(dev, gs->pwm_i2c_addr, ctrl_reg, val, 1 << shift);
729 if (ret)
730 return ret;
731
732 return 0;
733 }
734
735 /*
736 * Some boards contain a hw-bug where turning eldo2 back on after having turned
737 * it off causes the CPLM3218 ambient-light-sensor on the image-sensor's I2C bus
738 * to crash, hanging the bus. Do not turn eldo2 off on these systems.
739 */
740 static const struct dmi_system_id axp_leave_eldo2_on_ids[] = {
741 {
742 .matches = {
743 DMI_MATCH(DMI_SYS_VENDOR, "TrekStor"),
744 DMI_MATCH(DMI_PRODUCT_NAME, "SurfTab duo W1 10.1 (VT4)"),
745 },
746 },
747 { }
748 };
749
axp_v1p8_on(struct device * dev,struct gmin_subdev * gs)750 static int axp_v1p8_on(struct device *dev, struct gmin_subdev *gs)
751 {
752 int ret;
753
754 ret = axp_regulator_set(dev, gs, gs->eldo2_sel_reg, gs->eldo2_1p8v,
755 ELDO_CTRL_REG, gs->eldo2_ctrl_shift, true);
756 if (ret)
757 return ret;
758
759 /*
760 * This sleep comes out of the gc2235 driver, which is the
761 * only one I currently see that wants to set both 1.8v rails.
762 */
763 usleep_range(110, 150);
764
765 ret = axp_regulator_set(dev, gs, gs->eldo1_sel_reg, gs->eldo1_1p6v,
766 ELDO_CTRL_REG, gs->eldo1_ctrl_shift, true);
767 return ret;
768 }
769
axp_v1p8_off(struct device * dev,struct gmin_subdev * gs)770 static int axp_v1p8_off(struct device *dev, struct gmin_subdev *gs)
771 {
772 int ret;
773
774 ret = axp_regulator_set(dev, gs, gs->eldo1_sel_reg, gs->eldo1_1p6v,
775 ELDO_CTRL_REG, gs->eldo1_ctrl_shift, false);
776 if (ret)
777 return ret;
778
779 if (dmi_check_system(axp_leave_eldo2_on_ids))
780 return 0;
781
782 ret = axp_regulator_set(dev, gs, gs->eldo2_sel_reg, gs->eldo2_1p8v,
783 ELDO_CTRL_REG, gs->eldo2_ctrl_shift, false);
784 return ret;
785 }
786
gmin_gpio0_ctrl(struct v4l2_subdev * subdev,int on)787 static int gmin_gpio0_ctrl(struct v4l2_subdev *subdev, int on)
788 {
789 struct gmin_subdev *gs = find_gmin_subdev(subdev);
790
791 if (gs) {
792 gpiod_set_value(gs->gpio0, on);
793 return 0;
794 }
795 return -EINVAL;
796 }
797
gmin_gpio1_ctrl(struct v4l2_subdev * subdev,int on)798 static int gmin_gpio1_ctrl(struct v4l2_subdev *subdev, int on)
799 {
800 struct gmin_subdev *gs = find_gmin_subdev(subdev);
801
802 if (gs) {
803 gpiod_set_value(gs->gpio1, on);
804 return 0;
805 }
806 return -EINVAL;
807 }
808
gmin_v1p2_ctrl(struct v4l2_subdev * subdev,int on)809 static int gmin_v1p2_ctrl(struct v4l2_subdev *subdev, int on)
810 {
811 struct gmin_subdev *gs = find_gmin_subdev(subdev);
812
813 if (!gs || gs->v1p2_on == on)
814 return 0;
815 gs->v1p2_on = on;
816
817 /* use regulator for PMIC */
818 if (gs->v1p2_reg) {
819 if (on)
820 return regulator_enable(gs->v1p2_reg);
821 else
822 return regulator_disable(gs->v1p2_reg);
823 }
824
825 /* TODO:v1p2 may need to extend to other PMICs */
826
827 return -EINVAL;
828 }
829
gmin_v1p8_ctrl(struct v4l2_subdev * subdev,int on)830 static int gmin_v1p8_ctrl(struct v4l2_subdev *subdev, int on)
831 {
832 struct gmin_subdev *gs = find_gmin_subdev(subdev);
833 int ret;
834 int value;
835 int reg;
836
837 if (!gs || gs->v1p8_on == on)
838 return 0;
839
840 if (gs->v1p8_gpio >= 0) {
841 pr_info("atomisp_gmin_platform: 1.8v power on GPIO %d\n",
842 gs->v1p8_gpio);
843 ret = gpio_request(gs->v1p8_gpio, "camera_v1p8_en");
844 if (!ret)
845 ret = gpio_direction_output(gs->v1p8_gpio, 0);
846 if (ret)
847 pr_err("V1P8 GPIO initialization failed\n");
848 }
849
850 gs->v1p8_on = on;
851
852 ret = 0;
853 mutex_lock(&gmin_regulator_mutex);
854 if (on) {
855 gmin_v1p8_enable_count++;
856 if (gmin_v1p8_enable_count > 1)
857 goto out; /* Already on */
858 } else {
859 gmin_v1p8_enable_count--;
860 if (gmin_v1p8_enable_count > 0)
861 goto out; /* Still needed */
862 }
863
864 if (gs->v1p8_gpio >= 0)
865 gpio_set_value(gs->v1p8_gpio, on);
866
867 if (gs->v1p8_reg) {
868 regulator_set_voltage(gs->v1p8_reg, 1800000, 1800000);
869 if (on)
870 ret = regulator_enable(gs->v1p8_reg);
871 else
872 ret = regulator_disable(gs->v1p8_reg);
873
874 goto out;
875 }
876
877 switch (pmic_id) {
878 case PMIC_AXP:
879 if (on)
880 ret = axp_v1p8_on(subdev->dev, gs);
881 else
882 ret = axp_v1p8_off(subdev->dev, gs);
883 break;
884 case PMIC_TI:
885 value = on ? LDO_1P8V_ON : LDO_1P8V_OFF;
886
887 ret = gmin_i2c_write(subdev->dev, gs->pwm_i2c_addr,
888 LDO10_REG, value, 0xff);
889 break;
890 case PMIC_CRYSTALCOVE:
891 if (IS_ISP2401)
892 reg = CRYSTAL_CHT_1P8V_REG;
893 else
894 reg = CRYSTAL_BYT_1P8V_REG;
895
896 value = on ? CRYSTAL_ON : CRYSTAL_OFF;
897
898 ret = gmin_i2c_write(subdev->dev, gs->pwm_i2c_addr,
899 reg, value, 0xff);
900 break;
901 default:
902 dev_err(subdev->dev, "Couldn't set power mode for v1p8\n");
903 ret = -EINVAL;
904 }
905
906 out:
907 mutex_unlock(&gmin_regulator_mutex);
908 return ret;
909 }
910
gmin_v2p8_ctrl(struct v4l2_subdev * subdev,int on)911 static int gmin_v2p8_ctrl(struct v4l2_subdev *subdev, int on)
912 {
913 struct gmin_subdev *gs = find_gmin_subdev(subdev);
914 int ret;
915 int value;
916 int reg;
917
918 if (WARN_ON(!gs))
919 return -ENODEV;
920
921 if (gs->v2p8_gpio >= 0) {
922 pr_info("atomisp_gmin_platform: 2.8v power on GPIO %d\n",
923 gs->v2p8_gpio);
924 ret = gpio_request(gs->v2p8_gpio, "camera_v2p8");
925 if (!ret)
926 ret = gpio_direction_output(gs->v2p8_gpio, 0);
927 if (ret)
928 pr_err("V2P8 GPIO initialization failed\n");
929 }
930
931 if (gs->v2p8_on == on)
932 return 0;
933 gs->v2p8_on = on;
934
935 ret = 0;
936 mutex_lock(&gmin_regulator_mutex);
937 if (on) {
938 gmin_v2p8_enable_count++;
939 if (gmin_v2p8_enable_count > 1)
940 goto out; /* Already on */
941 } else {
942 gmin_v2p8_enable_count--;
943 if (gmin_v2p8_enable_count > 0)
944 goto out; /* Still needed */
945 }
946
947 if (gs->v2p8_gpio >= 0)
948 gpio_set_value(gs->v2p8_gpio, on);
949
950 if (gs->v2p8_reg) {
951 regulator_set_voltage(gs->v2p8_reg, 2900000, 2900000);
952 if (on)
953 ret = regulator_enable(gs->v2p8_reg);
954 else
955 ret = regulator_disable(gs->v2p8_reg);
956
957 goto out;
958 }
959
960 switch (pmic_id) {
961 case PMIC_AXP:
962 ret = axp_regulator_set(subdev->dev, gs, ALDO1_SEL_REG,
963 ALDO1_2P8V, ALDO1_CTRL3_REG,
964 ALDO1_CTRL3_SHIFT, on);
965 break;
966 case PMIC_TI:
967 value = on ? LDO_2P8V_ON : LDO_2P8V_OFF;
968
969 ret = gmin_i2c_write(subdev->dev, gs->pwm_i2c_addr,
970 LDO9_REG, value, 0xff);
971 break;
972 case PMIC_CRYSTALCOVE:
973 if (IS_ISP2401)
974 reg = CRYSTAL_CHT_2P8V_REG;
975 else
976 reg = CRYSTAL_BYT_2P8V_REG;
977
978 value = on ? CRYSTAL_ON : CRYSTAL_OFF;
979
980 ret = gmin_i2c_write(subdev->dev, gs->pwm_i2c_addr,
981 reg, value, 0xff);
982 break;
983 default:
984 dev_err(subdev->dev, "Couldn't set power mode for v2p8\n");
985 ret = -EINVAL;
986 }
987
988 out:
989 mutex_unlock(&gmin_regulator_mutex);
990 return ret;
991 }
992
gmin_acpi_pm_ctrl(struct v4l2_subdev * subdev,int on)993 static int gmin_acpi_pm_ctrl(struct v4l2_subdev *subdev, int on)
994 {
995 int ret = 0;
996 struct gmin_subdev *gs = find_gmin_subdev(subdev);
997 struct i2c_client *client = v4l2_get_subdevdata(subdev);
998 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
999
1000 /* Use the ACPI power management to control it */
1001 on = !!on;
1002 if (gs->clock_on == on)
1003 return 0;
1004
1005 dev_dbg(subdev->dev, "Setting power state to %s\n",
1006 on ? "on" : "off");
1007
1008 if (on)
1009 ret = acpi_device_set_power(adev,
1010 ACPI_STATE_D0);
1011 else
1012 ret = acpi_device_set_power(adev,
1013 ACPI_STATE_D3_COLD);
1014
1015 if (!ret)
1016 gs->clock_on = on;
1017 else
1018 dev_err(subdev->dev, "Couldn't set power state to %s\n",
1019 on ? "on" : "off");
1020
1021 return ret;
1022 }
1023
gmin_flisclk_ctrl(struct v4l2_subdev * subdev,int on)1024 static int gmin_flisclk_ctrl(struct v4l2_subdev *subdev, int on)
1025 {
1026 int ret = 0;
1027 struct gmin_subdev *gs = find_gmin_subdev(subdev);
1028 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1029
1030 if (gs->clock_on == !!on)
1031 return 0;
1032
1033 if (on) {
1034 ret = clk_set_rate(gs->pmc_clk,
1035 gs->clock_src ? CLK_RATE_19_2MHZ : CLK_RATE_25_0MHZ);
1036
1037 if (ret)
1038 dev_err(&client->dev, "unable to set PMC rate %d\n",
1039 gs->clock_src);
1040
1041 ret = clk_prepare_enable(gs->pmc_clk);
1042 if (ret == 0)
1043 gs->clock_on = true;
1044 } else {
1045 clk_disable_unprepare(gs->pmc_clk);
1046 gs->clock_on = false;
1047 }
1048
1049 return ret;
1050 }
1051
camera_sensor_csi_alloc(struct v4l2_subdev * sd,u32 port,u32 lanes,u32 format,u32 bayer_order)1052 static int camera_sensor_csi_alloc(struct v4l2_subdev *sd, u32 port, u32 lanes,
1053 u32 format, u32 bayer_order)
1054 {
1055 struct i2c_client *client = v4l2_get_subdevdata(sd);
1056 struct camera_mipi_info *csi;
1057
1058 csi = kzalloc(sizeof(*csi), GFP_KERNEL);
1059 if (!csi)
1060 return -ENOMEM;
1061
1062 csi->port = port;
1063 csi->num_lanes = lanes;
1064 csi->input_format = format;
1065 csi->raw_bayer_order = bayer_order;
1066 v4l2_set_subdev_hostdata(sd, csi);
1067 csi->metadata_format = ATOMISP_INPUT_FORMAT_EMBEDDED;
1068 csi->metadata_effective_width = NULL;
1069 dev_info(&client->dev,
1070 "camera pdata: port: %d lanes: %d order: %8.8x\n",
1071 port, lanes, bayer_order);
1072
1073 return 0;
1074 }
1075
camera_sensor_csi_free(struct v4l2_subdev * sd)1076 static void camera_sensor_csi_free(struct v4l2_subdev *sd)
1077 {
1078 struct camera_mipi_info *csi;
1079
1080 csi = v4l2_get_subdev_hostdata(sd);
1081 kfree(csi);
1082 }
1083
gmin_csi_cfg(struct v4l2_subdev * sd,int flag)1084 static int gmin_csi_cfg(struct v4l2_subdev *sd, int flag)
1085 {
1086 struct i2c_client *client = v4l2_get_subdevdata(sd);
1087 struct gmin_subdev *gs = find_gmin_subdev(sd);
1088
1089 if (!client || !gs)
1090 return -ENODEV;
1091
1092 if (flag)
1093 return camera_sensor_csi_alloc(sd, gs->csi_port, gs->csi_lanes,
1094 gs->csi_fmt, gs->csi_bayer);
1095 camera_sensor_csi_free(sd);
1096 return 0;
1097 }
1098
atomisp_register_sensor_no_gmin(struct v4l2_subdev * subdev,u32 lanes,enum atomisp_input_format format,enum atomisp_bayer_order bayer_order)1099 int atomisp_register_sensor_no_gmin(struct v4l2_subdev *subdev, u32 lanes,
1100 enum atomisp_input_format format,
1101 enum atomisp_bayer_order bayer_order)
1102 {
1103 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1104 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
1105 int i, ret, clock_num, port = 0;
1106
1107 if (adev) {
1108 /* Get ACPI _PR0 derived clock to determine the csi_port default */
1109 if (acpi_device_power_manageable(adev)) {
1110 clock_num = atomisp_get_acpi_power(&client->dev);
1111
1112 /* Compare clock to CsiPort 1 pmc-clock used in the CHT/BYT reference designs */
1113 if (IS_ISP2401)
1114 port = clock_num == 4 ? 1 : 0;
1115 else
1116 port = clock_num == 0 ? 1 : 0;
1117 }
1118
1119 port = gmin_get_var_int(&client->dev, false, "CsiPort", port);
1120 lanes = gmin_get_var_int(&client->dev, false, "CsiLanes", lanes);
1121 }
1122
1123 for (i = 0; i < MAX_SUBDEVS; i++)
1124 if (!pdata_subdevs[i].subdev)
1125 break;
1126
1127 if (i >= MAX_SUBDEVS) {
1128 dev_err(&client->dev, "Error too many subdevs already registered\n");
1129 return -ENOMEM;
1130 }
1131
1132 ret = camera_sensor_csi_alloc(subdev, port, lanes, format, bayer_order);
1133 if (ret)
1134 return ret;
1135
1136 pdata_subdevs[i].port = port;
1137 pdata_subdevs[i].lanes = lanes;
1138 pdata_subdevs[i].subdev = subdev;
1139 return 0;
1140 }
1141 EXPORT_SYMBOL_GPL(atomisp_register_sensor_no_gmin);
1142
atomisp_unregister_subdev(struct v4l2_subdev * subdev)1143 void atomisp_unregister_subdev(struct v4l2_subdev *subdev)
1144 {
1145 int i;
1146
1147 for (i = 0; i < MAX_SUBDEVS; i++) {
1148 if (pdata_subdevs[i].subdev != subdev)
1149 continue;
1150
1151 camera_sensor_csi_free(subdev);
1152 pdata_subdevs[i].subdev = NULL;
1153 pdata_subdevs[i].port = 0;
1154 break;
1155 }
1156 }
1157 EXPORT_SYMBOL_GPL(atomisp_unregister_subdev);
1158
1159 static struct camera_sensor_platform_data pmic_gmin_plat = {
1160 .gpio0_ctrl = gmin_gpio0_ctrl,
1161 .gpio1_ctrl = gmin_gpio1_ctrl,
1162 .v1p8_ctrl = gmin_v1p8_ctrl,
1163 .v2p8_ctrl = gmin_v2p8_ctrl,
1164 .v1p2_ctrl = gmin_v1p2_ctrl,
1165 .flisclk_ctrl = gmin_flisclk_ctrl,
1166 .csi_cfg = gmin_csi_cfg,
1167 };
1168
1169 static struct camera_sensor_platform_data acpi_gmin_plat = {
1170 .gpio0_ctrl = gmin_gpio0_ctrl,
1171 .gpio1_ctrl = gmin_gpio1_ctrl,
1172 .v1p8_ctrl = gmin_acpi_pm_ctrl,
1173 .v2p8_ctrl = gmin_acpi_pm_ctrl,
1174 .v1p2_ctrl = gmin_acpi_pm_ctrl,
1175 .flisclk_ctrl = gmin_acpi_pm_ctrl,
1176 .csi_cfg = gmin_csi_cfg,
1177 };
1178
1179 struct camera_sensor_platform_data *
gmin_camera_platform_data(struct v4l2_subdev * subdev,enum atomisp_input_format csi_format,enum atomisp_bayer_order csi_bayer)1180 gmin_camera_platform_data(struct v4l2_subdev *subdev,
1181 enum atomisp_input_format csi_format,
1182 enum atomisp_bayer_order csi_bayer)
1183 {
1184 u8 pmic_i2c_addr = gmin_detect_pmic(subdev);
1185 struct gmin_subdev *gs;
1186
1187 gs = find_free_gmin_subdev_slot();
1188 gs->subdev = subdev;
1189 gs->csi_fmt = csi_format;
1190 gs->csi_bayer = csi_bayer;
1191 gs->pwm_i2c_addr = pmic_i2c_addr;
1192
1193 gmin_subdev_add(gs);
1194 if (gs->pmc_clk)
1195 return &pmic_gmin_plat;
1196 else
1197 return &acpi_gmin_plat;
1198 }
1199 EXPORT_SYMBOL_GPL(gmin_camera_platform_data);
1200
gmin_get_hardcoded_var(struct device * dev,struct gmin_cfg_var * varlist,const char * var8,char * out,size_t * out_len)1201 static int gmin_get_hardcoded_var(struct device *dev,
1202 struct gmin_cfg_var *varlist,
1203 const char *var8, char *out, size_t *out_len)
1204 {
1205 struct gmin_cfg_var *gv;
1206
1207 for (gv = varlist; gv->name; gv++) {
1208 size_t vl;
1209
1210 if (strcmp(var8, gv->name))
1211 continue;
1212
1213 dev_info(dev, "Found DMI entry for '%s'\n", var8);
1214
1215 vl = strlen(gv->val);
1216 if (vl > *out_len - 1)
1217 return -ENOSPC;
1218
1219 strscpy(out, gv->val, *out_len);
1220 *out_len = vl;
1221 return 0;
1222 }
1223
1224 return -EINVAL;
1225 }
1226
1227
gmin_get_config_dsm_var(struct device * dev,const char * var,char * out,size_t * out_len)1228 static int gmin_get_config_dsm_var(struct device *dev,
1229 const char *var,
1230 char *out, size_t *out_len)
1231 {
1232 acpi_handle handle = ACPI_HANDLE(dev);
1233 union acpi_object *obj, *cur = NULL;
1234 int i;
1235
1236 /*
1237 * The data reported by "CamClk" seems to be either 0 or 1 at the
1238 * _DSM table.
1239 *
1240 * At the ACPI tables we looked so far, this is not related to the
1241 * actual clock source for the sensor, which is given by the
1242 * _PR0 ACPI table. So, ignore it, as otherwise this will be
1243 * set to a wrong value.
1244 */
1245 if (!strcmp(var, "CamClk"))
1246 return -EINVAL;
1247
1248 /* Return on unexpected object type */
1249 obj = acpi_evaluate_dsm_typed(handle, &atomisp_dsm_guid, 0, 0, NULL,
1250 ACPI_TYPE_PACKAGE);
1251 if (!obj) {
1252 dev_info_once(dev, "Didn't find ACPI _DSM table.\n");
1253 return -EINVAL;
1254 }
1255
1256 #if 0 /* Just for debugging purposes */
1257 for (i = 0; i < obj->package.count; i++) {
1258 union acpi_object *cur = &obj->package.elements[i];
1259
1260 if (cur->type == ACPI_TYPE_INTEGER)
1261 dev_info(dev, "object #%d, type %d, value: %lld\n",
1262 i, cur->type, cur->integer.value);
1263 else if (cur->type == ACPI_TYPE_STRING)
1264 dev_info(dev, "object #%d, type %d, string: %s\n",
1265 i, cur->type, cur->string.pointer);
1266 else
1267 dev_info(dev, "object #%d, type %d\n",
1268 i, cur->type);
1269 }
1270 #endif
1271
1272 /* Seek for the desired var */
1273 for (i = 0; i < obj->package.count - 1; i += 2) {
1274 if (obj->package.elements[i].type == ACPI_TYPE_STRING &&
1275 !strcmp(obj->package.elements[i].string.pointer, var)) {
1276 /* Next element should be the required value */
1277 cur = &obj->package.elements[i + 1];
1278 break;
1279 }
1280 }
1281
1282 if (!cur) {
1283 dev_info(dev, "didn't found _DSM entry for '%s'\n", var);
1284 ACPI_FREE(obj);
1285 return -EINVAL;
1286 }
1287
1288 /*
1289 * While it could be possible to have an ACPI_TYPE_INTEGER,
1290 * and read the value from cur->integer.value, the table
1291 * seen so far uses the string type. So, produce a warning
1292 * if it founds something different than string, letting it
1293 * to fall back to the old code.
1294 */
1295 if (cur && cur->type != ACPI_TYPE_STRING) {
1296 dev_info(dev, "found non-string _DSM entry for '%s'\n", var);
1297 ACPI_FREE(obj);
1298 return -EINVAL;
1299 }
1300
1301 dev_info(dev, "found _DSM entry for '%s': %s\n", var,
1302 cur->string.pointer);
1303 strscpy(out, cur->string.pointer, *out_len);
1304 *out_len = strlen(out);
1305
1306 ACPI_FREE(obj);
1307 return 0;
1308 }
1309
1310 /* Retrieves a device-specific configuration variable. The dev
1311 * argument should be a device with an ACPI companion, as all
1312 * configuration is based on firmware ID.
1313 */
gmin_get_config_var(struct device * maindev,bool is_gmin,const char * var,char * out,size_t * out_len)1314 static int gmin_get_config_var(struct device *maindev,
1315 bool is_gmin,
1316 const char *var,
1317 char *out, size_t *out_len)
1318 {
1319 struct acpi_device *adev = ACPI_COMPANION(maindev);
1320 efi_char16_t var16[CFG_VAR_NAME_MAX];
1321 const struct dmi_system_id *id;
1322 char var8[CFG_VAR_NAME_MAX];
1323 efi_status_t status;
1324 int i, ret;
1325
1326 if (!is_gmin && adev)
1327 ret = snprintf(var8, sizeof(var8), "%s_%s", acpi_dev_name(adev), var);
1328 else
1329 ret = snprintf(var8, sizeof(var8), "gmin_%s", var);
1330
1331 if (ret < 0 || ret >= sizeof(var8) - 1)
1332 return -EINVAL;
1333
1334 /* DMI based quirks override both the _DSM table and EFI variables */
1335 id = dmi_first_match(gmin_vars);
1336 if (id) {
1337 ret = gmin_get_hardcoded_var(maindev, id->driver_data, var8,
1338 out, out_len);
1339 if (!ret)
1340 return 0;
1341 }
1342
1343 /* For sensors, try first to use the _DSM table */
1344 if (!is_gmin) {
1345 ret = gmin_get_config_dsm_var(maindev, var, out, out_len);
1346 if (!ret)
1347 return 0;
1348 }
1349
1350 /* Our variable names are ASCII by construction, but EFI names
1351 * are wide chars. Convert and zero-pad.
1352 */
1353 memset(var16, 0, sizeof(var16));
1354 for (i = 0; i < sizeof(var8) && var8[i]; i++)
1355 var16[i] = var8[i];
1356
1357 status = EFI_UNSUPPORTED;
1358 if (efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE))
1359 status = efi.get_variable(var16, &GMIN_CFG_VAR_EFI_GUID, NULL,
1360 (unsigned long *)out_len, out);
1361 if (status == EFI_SUCCESS)
1362 dev_info(maindev, "found EFI entry for '%s'\n", var8);
1363 else if (is_gmin)
1364 dev_info(maindev, "Failed to find EFI gmin variable %s\n", var8);
1365 else
1366 dev_info(maindev, "Failed to find EFI variable %s\n", var8);
1367
1368 return ret;
1369 }
1370
gmin_get_var_int(struct device * dev,bool is_gmin,const char * var,int def)1371 int gmin_get_var_int(struct device *dev, bool is_gmin, const char *var, int def)
1372 {
1373 char val[CFG_VAR_NAME_MAX + 1];
1374 size_t len = CFG_VAR_NAME_MAX;
1375 long result;
1376 int ret;
1377
1378 ret = gmin_get_config_var(dev, is_gmin, var, val, &len);
1379 if (!ret) {
1380 val[len] = 0;
1381 ret = kstrtol(val, 0, &result);
1382 } else {
1383 dev_info(dev, "%s: using default (%d)\n", var, def);
1384 }
1385
1386 return ret ? def : result;
1387 }
1388 EXPORT_SYMBOL_GPL(gmin_get_var_int);
1389
1390 /* PCI quirk: The BYT ISP advertises PCI runtime PM but it doesn't
1391 * work. Disable so the kernel framework doesn't hang the device
1392 * trying. The driver itself does direct calls to the PUNIT to manage
1393 * ISP power.
1394 */
isp_pm_cap_fixup(struct pci_dev * pdev)1395 static void isp_pm_cap_fixup(struct pci_dev *pdev)
1396 {
1397 dev_info(&pdev->dev, "Disabling PCI power management on camera ISP\n");
1398 pdev->pm_cap = 0;
1399 }
1400 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x0f38, isp_pm_cap_fixup);
1401
1402 MODULE_DESCRIPTION("Ancillary routines for binding ACPI devices");
1403 MODULE_LICENSE("GPL");
1404