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 	struct regulator *v2p8_vcm_reg;
79 	enum atomisp_camera_port csi_port;
80 	unsigned int csi_lanes;
81 	enum atomisp_input_format csi_fmt;
82 	enum atomisp_bayer_order csi_bayer;
83 
84 	bool clock_on;
85 	bool v1p8_on;
86 	bool v2p8_on;
87 	bool v1p2_on;
88 	bool v2p8_vcm_on;
89 
90 	int v1p8_gpio;
91 	int v2p8_gpio;
92 
93 	u8 pwm_i2c_addr;
94 
95 	/* For PMIC AXP */
96 	int eldo1_sel_reg, eldo1_1p6v, eldo1_ctrl_shift;
97 	int eldo2_sel_reg, eldo2_1p8v, eldo2_ctrl_shift;
98 };
99 
100 static struct gmin_subdev gmin_subdevs[MAX_SUBDEVS];
101 
102 /* ACPI HIDs for the PMICs that could be used by this driver */
103 #define PMIC_ACPI_AXP		"INT33F4"	/* XPower AXP288 PMIC */
104 #define PMIC_ACPI_TI		"INT33F5"	/* Dollar Cove TI PMIC */
105 #define PMIC_ACPI_CRYSTALCOVE	"INT33FD"	/* Crystal Cove PMIC */
106 
107 #define PMIC_PLATFORM_TI	"intel_soc_pmic_chtdc_ti"
108 
109 static enum {
110 	PMIC_UNSET = 0,
111 	PMIC_REGULATOR,
112 	PMIC_AXP,
113 	PMIC_TI,
114 	PMIC_CRYSTALCOVE
115 } pmic_id;
116 
117 static const char *pmic_name[] = {
118 	[PMIC_UNSET]		= "ACPI device PM",
119 	[PMIC_REGULATOR]	= "regulator driver",
120 	[PMIC_AXP]		= "XPower AXP288 PMIC",
121 	[PMIC_TI]		= "Dollar Cove TI PMIC",
122 	[PMIC_CRYSTALCOVE]	= "Crystal Cove PMIC",
123 };
124 
125 static DEFINE_MUTEX(gmin_regulator_mutex);
126 static int gmin_v1p8_enable_count;
127 static int gmin_v2p8_enable_count;
128 
129 /* The atomisp uses type==0 for the end-of-list marker, so leave space. */
130 static struct intel_v4l2_subdev_table pdata_subdevs[MAX_SUBDEVS + 1];
131 
132 static const struct atomisp_platform_data pdata = {
133 	.subdevs = pdata_subdevs,
134 };
135 
136 static LIST_HEAD(vcm_devices);
137 static DEFINE_MUTEX(vcm_lock);
138 
139 static struct gmin_subdev *find_gmin_subdev(struct v4l2_subdev *subdev);
140 
141 const struct atomisp_platform_data *atomisp_get_platform_data(void)
142 {
143 	return &pdata;
144 }
145 EXPORT_SYMBOL_GPL(atomisp_get_platform_data);
146 
147 int atomisp_register_i2c_module(struct v4l2_subdev *subdev,
148 				struct camera_sensor_platform_data *plat_data,
149 				enum intel_v4l2_subdev_type type)
150 {
151 	int i;
152 	struct gmin_subdev *gs;
153 	struct i2c_client *client = v4l2_get_subdevdata(subdev);
154 	struct acpi_device *adev = ACPI_COMPANION(&client->dev);
155 
156 	dev_info(&client->dev, "register atomisp i2c module type %d\n", type);
157 
158 	/* The windows driver model (and thus most BIOSes by default)
159 	 * uses ACPI runtime power management for camera devices, but
160 	 * we don't.  Disable it, or else the rails will be needlessly
161 	 * tickled during suspend/resume.  This has caused power and
162 	 * performance issues on multiple devices.
163 	 */
164 
165 	/*
166 	 * Turn off the device before disabling ACPI power resources
167 	 * (the sensor driver has already probed it at this point).
168 	 * This avoids leaking the reference count of the (possibly shared)
169 	 * ACPI power resources which were enabled/referenced before probe().
170 	 */
171 	acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
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 	if (!gs)
188 		return -ENODEV;
189 
190 	pdata.subdevs[i].type = type;
191 	pdata.subdevs[i].port = gs->csi_port;
192 	pdata.subdevs[i].subdev = subdev;
193 	return 0;
194 }
195 EXPORT_SYMBOL_GPL(atomisp_register_i2c_module);
196 
197 int atomisp_gmin_remove_subdev(struct v4l2_subdev *sd)
198 {
199 	int i, j;
200 
201 	if (!sd)
202 		return 0;
203 
204 	for (i = 0; i < MAX_SUBDEVS; i++) {
205 		if (pdata.subdevs[i].subdev == sd) {
206 			for (j = i + 1; j <= MAX_SUBDEVS; j++)
207 				pdata.subdevs[j - 1] = pdata.subdevs[j];
208 		}
209 		if (gmin_subdevs[i].subdev == sd) {
210 			if (gmin_subdevs[i].gpio0)
211 				gpiod_put(gmin_subdevs[i].gpio0);
212 			gmin_subdevs[i].gpio0 = NULL;
213 			if (gmin_subdevs[i].gpio1)
214 				gpiod_put(gmin_subdevs[i].gpio1);
215 			gmin_subdevs[i].gpio1 = NULL;
216 			if (pmic_id == PMIC_REGULATOR) {
217 				regulator_put(gmin_subdevs[i].v1p8_reg);
218 				regulator_put(gmin_subdevs[i].v2p8_reg);
219 				regulator_put(gmin_subdevs[i].v1p2_reg);
220 				regulator_put(gmin_subdevs[i].v2p8_vcm_reg);
221 			}
222 			gmin_subdevs[i].subdev = NULL;
223 		}
224 	}
225 	return 0;
226 }
227 EXPORT_SYMBOL_GPL(atomisp_gmin_remove_subdev);
228 
229 struct gmin_cfg_var {
230 	const char *name, *val;
231 };
232 
233 static struct gmin_cfg_var ffrd8_vars[] = {
234 	{ "INTCF1B:00_ImxId",    "0x134" },
235 	{ "INTCF1B:00_CsiPort",  "1" },
236 	{ "INTCF1B:00_CsiLanes", "4" },
237 	{ "INTCF1B:00_CamClk", "0" },
238 	{},
239 };
240 
241 /* Cribbed from MCG defaults in the mt9m114 driver, not actually verified
242  * vs. T100 hardware
243  */
244 static struct gmin_cfg_var t100_vars[] = {
245 	{ "INT33F0:00_CsiPort",  "0" },
246 	{ "INT33F0:00_CsiLanes", "1" },
247 	{ "INT33F0:00_CamClk",   "1" },
248 	{},
249 };
250 
251 static struct gmin_cfg_var mrd7_vars[] = {
252 	{"INT33F8:00_CamType", "1"},
253 	{"INT33F8:00_CsiPort", "1"},
254 	{"INT33F8:00_CsiLanes", "2"},
255 	{"INT33F8:00_CsiFmt", "13"},
256 	{"INT33F8:00_CsiBayer", "0"},
257 	{"INT33F8:00_CamClk", "0"},
258 
259 	{"INT33F9:00_CamType", "1"},
260 	{"INT33F9:00_CsiPort", "0"},
261 	{"INT33F9:00_CsiLanes", "1"},
262 	{"INT33F9:00_CsiFmt", "13"},
263 	{"INT33F9:00_CsiBayer", "0"},
264 	{"INT33F9:00_CamClk", "1"},
265 	{},
266 };
267 
268 static struct gmin_cfg_var ecs7_vars[] = {
269 	{"INT33BE:00_CsiPort", "1"},
270 	{"INT33BE:00_CsiLanes", "2"},
271 	{"INT33BE:00_CsiFmt", "13"},
272 	{"INT33BE:00_CsiBayer", "2"},
273 	{"INT33BE:00_CamClk", "0"},
274 
275 	{"INT33F0:00_CsiPort", "0"},
276 	{"INT33F0:00_CsiLanes", "1"},
277 	{"INT33F0:00_CsiFmt", "13"},
278 	{"INT33F0:00_CsiBayer", "0"},
279 	{"INT33F0:00_CamClk", "1"},
280 	{"gmin_V2P8GPIO", "402"},
281 	{},
282 };
283 
284 static struct gmin_cfg_var i8880_vars[] = {
285 	{"XXOV2680:00_CsiPort", "1"},
286 	{"XXOV2680:00_CsiLanes", "1"},
287 	{"XXOV2680:00_CamClk", "0"},
288 
289 	{"XXGC0310:00_CsiPort", "0"},
290 	{"XXGC0310:00_CsiLanes", "1"},
291 	{"XXGC0310:00_CamClk", "1"},
292 	{},
293 };
294 
295 /*
296  * Surface 3 does not describe CsiPort/CsiLanes in both DSDT and EFI.
297  */
298 static struct gmin_cfg_var surface3_vars[] = {
299 	{"APTA0330:00_CsiPort", "0"},
300 	{"APTA0330:00_CsiLanes", "2"},
301 
302 	{"OVTI8835:00_CsiPort", "1"},
303 	{"OVTI8835:00_CsiLanes", "4"},
304 	{},
305 };
306 
307 static struct gmin_cfg_var lenovo_ideapad_miix_310_vars[] = {
308 	/* _DSM contains the wrong CsiPort! */
309 	{ "OVTI2680:01_CsiPort", "0" },
310 	{}
311 };
312 
313 static const struct dmi_system_id gmin_vars[] = {
314 	/*
315 	 * These DMI IDs were present when the atomisp driver was merged into
316 	 * drivers/staging and it is unclear if they are really necessary.
317 	 */
318 	{
319 		.ident = "BYT-T FFD8",
320 		.matches = {
321 			DMI_MATCH(DMI_BOARD_NAME, "BYT-T FFD8"),
322 		},
323 		.driver_data = ffrd8_vars,
324 	},
325 	{
326 		.ident = "T100TA",
327 		.matches = {
328 			DMI_MATCH(DMI_BOARD_NAME, "T100TA"),
329 		},
330 		.driver_data = t100_vars,
331 	},
332 	{
333 		.ident = "MRD7",
334 		.matches = {
335 			DMI_MATCH(DMI_BOARD_NAME, "TABLET"),
336 			DMI_MATCH(DMI_BOARD_VERSION, "MRD 7"),
337 		},
338 		.driver_data = mrd7_vars,
339 	},
340 	{
341 		.ident = "ST70408",
342 		.matches = {
343 			DMI_MATCH(DMI_BOARD_NAME, "ST70408"),
344 		},
345 		.driver_data = ecs7_vars,
346 	},
347 	{
348 		.ident = "VTA0803",
349 		.matches = {
350 			DMI_MATCH(DMI_BOARD_NAME, "VTA0803"),
351 		},
352 		.driver_data = i8880_vars,
353 	},
354 	/* Later added DMI ids, these are confirmed to really be necessary! */
355 	{
356 		.ident = "Surface 3",
357 		.matches = {
358 			DMI_MATCH(DMI_BOARD_NAME, "Surface 3"),
359 		},
360 		.driver_data = surface3_vars,
361 	},
362 	{
363 		.ident = "Lenovo Ideapad Miix 310",
364 		.matches = {
365 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
366 			DMI_MATCH(DMI_PRODUCT_VERSION, "MIIX 310-10"),
367 		},
368 		.driver_data = lenovo_ideapad_miix_310_vars,
369 	},
370 	{}
371 };
372 
373 #define GMIN_CFG_VAR_EFI_GUID EFI_GUID(0xecb54cd9, 0xe5ae, 0x4fdc, \
374 				       0xa9, 0x71, 0xe8, 0x77,	   \
375 				       0x75, 0x60, 0x68, 0xf7)
376 
377 static const guid_t atomisp_dsm_guid = GUID_INIT(0xdc2f6c4f, 0x045b, 0x4f1d,
378 						 0x97, 0xb9, 0x88, 0x2a,
379 						 0x68, 0x60, 0xa4, 0xbe);
380 
381 #define CFG_VAR_NAME_MAX 64
382 
383 #define GMIN_PMC_CLK_NAME 14 /* "pmc_plt_clk_[0..5]" */
384 static char gmin_pmc_clk_name[GMIN_PMC_CLK_NAME];
385 
386 static struct i2c_client *gmin_i2c_dev_exists(struct device *dev, char *name,
387 					      struct i2c_client **client)
388 {
389 	struct acpi_device *adev;
390 	struct device *d;
391 
392 	adev = acpi_dev_get_first_match_dev(name, NULL, -1);
393 	if (!adev)
394 		return NULL;
395 
396 	d = bus_find_device_by_acpi_dev(&i2c_bus_type, adev);
397 	acpi_dev_put(adev);
398 	if (!d)
399 		return NULL;
400 
401 	*client = i2c_verify_client(d);
402 	put_device(d);
403 
404 	dev_dbg(dev, "found '%s' at address 0x%02x, adapter %d\n",
405 		(*client)->name, (*client)->addr, (*client)->adapter->nr);
406 	return *client;
407 }
408 
409 static int gmin_i2c_write(struct device *dev, u16 i2c_addr, u8 reg,
410 			  u32 value, u32 mask)
411 {
412 	int ret;
413 
414 	/*
415 	 * FIXME: Right now, the intel_pmic driver just write values
416 	 * directly at the regmap, instead of properly implementing
417 	 * i2c_transfer() mechanism. Let's use the same interface here,
418 	 * as otherwise we may face issues.
419 	 */
420 
421 	dev_dbg(dev,
422 		"I2C write, addr: 0x%02x, reg: 0x%02x, value: 0x%02x, mask: 0x%02x\n",
423 		i2c_addr, reg, value, mask);
424 
425 	ret = intel_soc_pmic_exec_mipi_pmic_seq_element(i2c_addr, reg, value, mask);
426 	if (ret == -EOPNOTSUPP)
427 		dev_err(dev,
428 			"ACPI didn't mapped the OpRegion needed to access I2C address 0x%02x.\n"
429 			"Need to compile the kernel using CONFIG_*_PMIC_OPREGION settings\n",
430 			i2c_addr);
431 
432 	return ret;
433 }
434 
435 static int atomisp_get_acpi_power(struct device *dev)
436 {
437 	char name[5];
438 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
439 	struct acpi_buffer b_name = { sizeof(name), name };
440 	union acpi_object *package, *element;
441 	acpi_handle handle = ACPI_HANDLE(dev);
442 	acpi_handle rhandle;
443 	acpi_status status;
444 	int clock_num = -1;
445 	int i;
446 
447 	status = acpi_evaluate_object(handle, "_PR0", NULL, &buffer);
448 	if (!ACPI_SUCCESS(status))
449 		return -1;
450 
451 	package = buffer.pointer;
452 
453 	if (!buffer.length || !package
454 	    || package->type != ACPI_TYPE_PACKAGE
455 	    || !package->package.count)
456 		goto fail;
457 
458 	for (i = 0; i < package->package.count; i++) {
459 		element = &package->package.elements[i];
460 
461 		if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
462 			continue;
463 
464 		rhandle = element->reference.handle;
465 		if (!rhandle)
466 			goto fail;
467 
468 		acpi_get_name(rhandle, ACPI_SINGLE_NAME, &b_name);
469 
470 		dev_dbg(dev, "Found PM resource '%s'\n", name);
471 		if (strlen(name) == 4 && !strncmp(name, "CLK", 3)) {
472 			if (name[3] >= '0' && name[3] <= '4')
473 				clock_num = name[3] - '0';
474 #if 0
475 			/*
476 			 * We could abort here, but let's parse all resources,
477 			 * as this is helpful for debugging purposes
478 			 */
479 			if (clock_num >= 0)
480 				break;
481 #endif
482 		}
483 	}
484 
485 fail:
486 	ACPI_FREE(buffer.pointer);
487 
488 	return clock_num;
489 }
490 
491 static u8 gmin_get_pmic_id_and_addr(struct device *dev)
492 {
493 	struct i2c_client *power = NULL;
494 	static u8 pmic_i2c_addr;
495 
496 	if (pmic_id)
497 		return pmic_i2c_addr;
498 
499 	if (gmin_i2c_dev_exists(dev, PMIC_ACPI_TI, &power))
500 		pmic_id = PMIC_TI;
501 	else if (gmin_i2c_dev_exists(dev, PMIC_ACPI_AXP, &power))
502 		pmic_id = PMIC_AXP;
503 	else if (gmin_i2c_dev_exists(dev, PMIC_ACPI_CRYSTALCOVE, &power))
504 		pmic_id = PMIC_CRYSTALCOVE;
505 	else
506 		pmic_id = PMIC_REGULATOR;
507 
508 	pmic_i2c_addr = power ? power->addr : 0;
509 	return pmic_i2c_addr;
510 }
511 
512 static int gmin_detect_pmic(struct v4l2_subdev *subdev)
513 {
514 	struct i2c_client *client = v4l2_get_subdevdata(subdev);
515 	struct device *dev = &client->dev;
516 	u8 pmic_i2c_addr;
517 
518 	pmic_i2c_addr = gmin_get_pmic_id_and_addr(dev);
519 	dev_info(dev, "gmin: power management provided via %s (i2c addr 0x%02x)\n",
520 		 pmic_name[pmic_id], pmic_i2c_addr);
521 	return pmic_i2c_addr;
522 }
523 
524 static int gmin_subdev_add(struct gmin_subdev *gs)
525 {
526 	struct i2c_client *client = v4l2_get_subdevdata(gs->subdev);
527 	struct device *dev = &client->dev;
528 	struct acpi_device *adev = ACPI_COMPANION(dev);
529 	int ret, default_val, clock_num = -1;
530 
531 	dev_info(dev, "%s: ACPI path is %pfw\n", __func__, dev_fwnode(dev));
532 
533 	/*WA:CHT requires XTAL clock as PLL is not stable.*/
534 	gs->clock_src = gmin_get_var_int(dev, false, "ClkSrc",
535 				         VLV2_CLK_PLL_19P2MHZ);
536 
537 	/*
538 	 * Get ACPI _PR0 derived clock here already because it is used
539 	 * to determine the csi_port default.
540 	 */
541 	if (acpi_device_power_manageable(adev))
542 		clock_num = atomisp_get_acpi_power(dev);
543 
544 	/* Compare clock to CsiPort 1 pmc-clock used in the CHT/BYT reference designs */
545 	if (IS_ISP2401)
546 		default_val = clock_num == 4 ? 1 : 0;
547 	else
548 		default_val = clock_num == 0 ? 1 : 0;
549 
550 	gs->csi_port = gmin_get_var_int(dev, false, "CsiPort", default_val);
551 	gs->csi_lanes = gmin_get_var_int(dev, false, "CsiLanes", 1);
552 
553 	gs->gpio0 = gpiod_get_index(dev, NULL, 0, GPIOD_OUT_LOW);
554 	if (IS_ERR(gs->gpio0))
555 		gs->gpio0 = NULL;
556 	else
557 		dev_info(dev, "will handle gpio0 via ACPI\n");
558 
559 	gs->gpio1 = gpiod_get_index(dev, NULL, 1, GPIOD_OUT_LOW);
560 	if (IS_ERR(gs->gpio1))
561 		gs->gpio1 = NULL;
562 	else
563 		dev_info(dev, "will handle gpio1 via ACPI\n");
564 
565 	/*
566 	 * Those are used only when there is an external regulator apart
567 	 * from the PMIC that would be providing power supply, like on the
568 	 * two cases below:
569 	 *
570 	 * The ECS E7 board drives camera 2.8v from an external regulator
571 	 * instead of the PMIC.  There's a gmin_CamV2P8 config variable
572 	 * that specifies the GPIO to handle this particular case,
573 	 * but this needs a broader architecture for handling camera power.
574 	 *
575 	 * The CHT RVP board drives camera 1.8v from an* external regulator
576 	 * instead of the PMIC just like ECS E7 board.
577 	 */
578 
579 	gs->v1p8_gpio = gmin_get_var_int(dev, true, "V1P8GPIO", -1);
580 	gs->v2p8_gpio = gmin_get_var_int(dev, true, "V2P8GPIO", -1);
581 
582 	/*
583 	 * FIXME:
584 	 *
585 	 * The ACPI handling code checks for the _PR? tables in order to
586 	 * know what is required to switch the device from power state
587 	 * D0 (_PR0) up to D3COLD (_PR3).
588 	 *
589 	 * The adev->flags.power_manageable is set to true if the device
590 	 * has a _PR0 table, which can be checked by calling
591 	 * acpi_device_power_manageable(adev).
592 	 *
593 	 * However, this only says that the device can be set to power off
594 	 * mode.
595 	 *
596 	 * At least on the DSDT tables we've seen so far, there's no _PR3,
597 	 * nor _PS3 (which would have a somewhat similar effect).
598 	 * So, using ACPI for power management won't work, except if adding
599 	 * an ACPI override logic somewhere.
600 	 *
601 	 * So, at least for the existing devices we know, the check below
602 	 * will always be false.
603 	 */
604 	if (acpi_device_can_wakeup(adev) &&
605 	    acpi_device_can_poweroff(adev)) {
606 		dev_info(dev,
607 			 "gmin: power management provided via device PM\n");
608 		return 0;
609 	}
610 
611 	/*
612 	 * The code below is here due to backward compatibility with devices
613 	 * whose ACPI BIOS may not contain everything that would be needed
614 	 * in order to set clocks and do power management.
615 	 */
616 
617 	/*
618 	 * According with :
619 	 *   https://github.com/projectceladon/hardware-intel-kernelflinger/blob/master/doc/fastboot.md
620 	 *
621 	 * The "CamClk" EFI var is set via fastboot on some Android devices,
622 	 * and seems to contain the number of the clock used to feed the
623 	 * sensor.
624 	 *
625 	 * On systems with a proper ACPI table, this is given via the _PR0
626 	 * power resource table. The logic below should first check if there
627 	 * is a power resource already, falling back to the EFI vars detection
628 	 * otherwise.
629 	 */
630 
631 	/* If getting the clock from _PR0 above failed, fall-back to EFI and/or DMI match */
632 	if (clock_num < 0)
633 		clock_num = gmin_get_var_int(dev, false, "CamClk", 0);
634 
635 	if (clock_num < 0 || clock_num > MAX_CLK_COUNT) {
636 		dev_err(dev, "Invalid clock number\n");
637 		return -EINVAL;
638 	}
639 
640 	snprintf(gmin_pmc_clk_name, sizeof(gmin_pmc_clk_name),
641 		 "%s_%d", "pmc_plt_clk", clock_num);
642 
643 	gs->pmc_clk = devm_clk_get(dev, gmin_pmc_clk_name);
644 	if (IS_ERR(gs->pmc_clk)) {
645 		ret = PTR_ERR(gs->pmc_clk);
646 		dev_err(dev, "Failed to get clk from %s: %d\n", gmin_pmc_clk_name, ret);
647 		return ret;
648 	}
649 	dev_info(dev, "Will use CLK%d (%s)\n", clock_num, gmin_pmc_clk_name);
650 
651 	/*
652 	 * The firmware might enable the clock at
653 	 * boot (this information may or may not
654 	 * be reflected in the enable clock register).
655 	 * To change the rate we must disable the clock
656 	 * first to cover these cases. Due to common
657 	 * clock framework restrictions that do not allow
658 	 * to disable a clock that has not been enabled,
659 	 * we need to enable the clock first.
660 	 */
661 	ret = clk_prepare_enable(gs->pmc_clk);
662 	if (!ret)
663 		clk_disable_unprepare(gs->pmc_clk);
664 
665 	switch (pmic_id) {
666 	case PMIC_REGULATOR:
667 		gs->v1p8_reg = regulator_get(dev, "V1P8SX");
668 		gs->v2p8_reg = regulator_get(dev, "V2P8SX");
669 
670 		gs->v1p2_reg = regulator_get(dev, "V1P2A");
671 		gs->v2p8_vcm_reg = regulator_get(dev, "VPROG4B");
672 
673 		/* Note: ideally we would initialize v[12]p8_on to the
674 		 * output of regulator_is_enabled(), but sadly that
675 		 * API is broken with the current drivers, returning
676 		 * "1" for a regulator that will then emit a
677 		 * "unbalanced disable" WARNing if we try to disable
678 		 * it.
679 		 */
680 		break;
681 
682 	case PMIC_AXP:
683 		gs->eldo1_1p6v = gmin_get_var_int(dev, false,
684 						  "eldo1_1p8v",
685 						  ELDO1_1P6V);
686 		gs->eldo1_sel_reg = gmin_get_var_int(dev, false,
687 						     "eldo1_sel_reg",
688 						     ELDO1_SEL_REG);
689 		gs->eldo1_ctrl_shift = gmin_get_var_int(dev, false,
690 							"eldo1_ctrl_shift",
691 							ELDO1_CTRL_SHIFT);
692 		gs->eldo2_1p8v = gmin_get_var_int(dev, false,
693 						  "eldo2_1p8v",
694 						  ELDO2_1P8V);
695 		gs->eldo2_sel_reg = gmin_get_var_int(dev, false,
696 						     "eldo2_sel_reg",
697 						     ELDO2_SEL_REG);
698 		gs->eldo2_ctrl_shift = gmin_get_var_int(dev, false,
699 							"eldo2_ctrl_shift",
700 							ELDO2_CTRL_SHIFT);
701 		break;
702 
703 	default:
704 		break;
705 	}
706 
707 	return 0;
708 }
709 
710 static struct gmin_subdev *find_gmin_subdev(struct v4l2_subdev *subdev)
711 {
712 	int i;
713 
714 	for (i = 0; i < MAX_SUBDEVS; i++)
715 		if (gmin_subdevs[i].subdev == subdev)
716 			return &gmin_subdevs[i];
717 	return NULL;
718 }
719 
720 static struct gmin_subdev *find_free_gmin_subdev_slot(void)
721 {
722 	unsigned int i;
723 
724 	for (i = 0; i < MAX_SUBDEVS; i++)
725 		if (gmin_subdevs[i].subdev == NULL)
726 			return &gmin_subdevs[i];
727 	return NULL;
728 }
729 
730 static int axp_regulator_set(struct device *dev, struct gmin_subdev *gs,
731 			     int sel_reg, u8 setting,
732 			     int ctrl_reg, int shift, bool on)
733 {
734 	int ret;
735 	int val;
736 
737 	ret = gmin_i2c_write(dev, gs->pwm_i2c_addr, sel_reg, setting, 0xff);
738 	if (ret)
739 		return ret;
740 
741 	val = on ? 1 << shift : 0;
742 
743 	ret = gmin_i2c_write(dev, gs->pwm_i2c_addr, ctrl_reg, val, 1 << shift);
744 	if (ret)
745 		return ret;
746 
747 	return 0;
748 }
749 
750 /*
751  * Some boards contain a hw-bug where turning eldo2 back on after having turned
752  * it off causes the CPLM3218 ambient-light-sensor on the image-sensor's I2C bus
753  * to crash, hanging the bus. Do not turn eldo2 off on these systems.
754  */
755 static const struct dmi_system_id axp_leave_eldo2_on_ids[] = {
756 	{
757 		.matches = {
758 			DMI_MATCH(DMI_SYS_VENDOR, "TrekStor"),
759 			DMI_MATCH(DMI_PRODUCT_NAME, "SurfTab duo W1 10.1 (VT4)"),
760 		},
761 	},
762 	{ }
763 };
764 
765 static int axp_v1p8_on(struct device *dev, struct gmin_subdev *gs)
766 {
767 	int ret;
768 
769 	ret = axp_regulator_set(dev, gs, gs->eldo2_sel_reg, gs->eldo2_1p8v,
770 				ELDO_CTRL_REG, gs->eldo2_ctrl_shift, true);
771 	if (ret)
772 		return ret;
773 
774 	/*
775 	 * This sleep comes out of the gc2235 driver, which is the
776 	 * only one I currently see that wants to set both 1.8v rails.
777 	 */
778 	usleep_range(110, 150);
779 
780 	ret = axp_regulator_set(dev, gs, gs->eldo1_sel_reg, gs->eldo1_1p6v,
781 				ELDO_CTRL_REG, gs->eldo1_ctrl_shift, true);
782 	return ret;
783 }
784 
785 static int axp_v1p8_off(struct device *dev, struct gmin_subdev *gs)
786 {
787 	int ret;
788 
789 	ret = axp_regulator_set(dev, gs, gs->eldo1_sel_reg, gs->eldo1_1p6v,
790 				ELDO_CTRL_REG, gs->eldo1_ctrl_shift, false);
791 	if (ret)
792 		return ret;
793 
794 	if (dmi_check_system(axp_leave_eldo2_on_ids))
795 		return 0;
796 
797 	ret = axp_regulator_set(dev, gs, gs->eldo2_sel_reg, gs->eldo2_1p8v,
798 				ELDO_CTRL_REG, gs->eldo2_ctrl_shift, false);
799 	return ret;
800 }
801 
802 static int gmin_gpio0_ctrl(struct v4l2_subdev *subdev, int on)
803 {
804 	struct gmin_subdev *gs = find_gmin_subdev(subdev);
805 
806 	if (gs) {
807 		gpiod_set_value(gs->gpio0, on);
808 		return 0;
809 	}
810 	return -EINVAL;
811 }
812 
813 static int gmin_gpio1_ctrl(struct v4l2_subdev *subdev, int on)
814 {
815 	struct gmin_subdev *gs = find_gmin_subdev(subdev);
816 
817 	if (gs) {
818 		gpiod_set_value(gs->gpio1, on);
819 		return 0;
820 	}
821 	return -EINVAL;
822 }
823 
824 static int gmin_v1p2_ctrl(struct v4l2_subdev *subdev, int on)
825 {
826 	struct gmin_subdev *gs = find_gmin_subdev(subdev);
827 
828 	if (!gs || gs->v1p2_on == on)
829 		return 0;
830 	gs->v1p2_on = on;
831 
832 	/* use regulator for PMIC */
833 	if (gs->v1p2_reg) {
834 		if (on)
835 			return regulator_enable(gs->v1p2_reg);
836 		else
837 			return regulator_disable(gs->v1p2_reg);
838 	}
839 
840 	/* TODO:v1p2 may need to extend to other PMICs */
841 
842 	return -EINVAL;
843 }
844 
845 static int gmin_v1p8_ctrl(struct v4l2_subdev *subdev, int on)
846 {
847 	struct gmin_subdev *gs = find_gmin_subdev(subdev);
848 	int ret;
849 	int value;
850 	int reg;
851 
852 	if (!gs || gs->v1p8_on == on)
853 		return 0;
854 
855 	if (gs->v1p8_gpio >= 0) {
856 		pr_info("atomisp_gmin_platform: 1.8v power on GPIO %d\n",
857 			gs->v1p8_gpio);
858 		ret = gpio_request(gs->v1p8_gpio, "camera_v1p8_en");
859 		if (!ret)
860 			ret = gpio_direction_output(gs->v1p8_gpio, 0);
861 		if (ret)
862 			pr_err("V1P8 GPIO initialization failed\n");
863 	}
864 
865 	gs->v1p8_on = on;
866 
867 	ret = 0;
868 	mutex_lock(&gmin_regulator_mutex);
869 	if (on) {
870 		gmin_v1p8_enable_count++;
871 		if (gmin_v1p8_enable_count > 1)
872 			goto out; /* Already on */
873 	} else {
874 		gmin_v1p8_enable_count--;
875 		if (gmin_v1p8_enable_count > 0)
876 			goto out; /* Still needed */
877 	}
878 
879 	if (gs->v1p8_gpio >= 0)
880 		gpio_set_value(gs->v1p8_gpio, on);
881 
882 	if (gs->v1p8_reg) {
883 		regulator_set_voltage(gs->v1p8_reg, 1800000, 1800000);
884 		if (on)
885 			ret = regulator_enable(gs->v1p8_reg);
886 		else
887 			ret = regulator_disable(gs->v1p8_reg);
888 
889 		goto out;
890 	}
891 
892 	switch (pmic_id) {
893 	case PMIC_AXP:
894 		if (on)
895 			ret = axp_v1p8_on(subdev->dev, gs);
896 		else
897 			ret = axp_v1p8_off(subdev->dev, gs);
898 		break;
899 	case PMIC_TI:
900 		value = on ? LDO_1P8V_ON : LDO_1P8V_OFF;
901 
902 		ret = gmin_i2c_write(subdev->dev, gs->pwm_i2c_addr,
903 				     LDO10_REG, value, 0xff);
904 		break;
905 	case PMIC_CRYSTALCOVE:
906 		if (IS_ISP2401)
907 			reg = CRYSTAL_CHT_1P8V_REG;
908 		else
909 			reg = CRYSTAL_BYT_1P8V_REG;
910 
911 		value = on ? CRYSTAL_ON : CRYSTAL_OFF;
912 
913 		ret = gmin_i2c_write(subdev->dev, gs->pwm_i2c_addr,
914 				     reg, value, 0xff);
915 		break;
916 	default:
917 		dev_err(subdev->dev, "Couldn't set power mode for v1p8\n");
918 		ret = -EINVAL;
919 	}
920 
921 out:
922 	mutex_unlock(&gmin_regulator_mutex);
923 	return ret;
924 }
925 
926 static int gmin_v2p8_ctrl(struct v4l2_subdev *subdev, int on)
927 {
928 	struct gmin_subdev *gs = find_gmin_subdev(subdev);
929 	int ret;
930 	int value;
931 	int reg;
932 
933 	if (WARN_ON(!gs))
934 		return -ENODEV;
935 
936 	if (gs->v2p8_gpio >= 0) {
937 		pr_info("atomisp_gmin_platform: 2.8v power on GPIO %d\n",
938 			gs->v2p8_gpio);
939 		ret = gpio_request(gs->v2p8_gpio, "camera_v2p8");
940 		if (!ret)
941 			ret = gpio_direction_output(gs->v2p8_gpio, 0);
942 		if (ret)
943 			pr_err("V2P8 GPIO initialization failed\n");
944 	}
945 
946 	if (gs->v2p8_on == on)
947 		return 0;
948 	gs->v2p8_on = on;
949 
950 	ret = 0;
951 	mutex_lock(&gmin_regulator_mutex);
952 	if (on) {
953 		gmin_v2p8_enable_count++;
954 		if (gmin_v2p8_enable_count > 1)
955 			goto out; /* Already on */
956 	} else {
957 		gmin_v2p8_enable_count--;
958 		if (gmin_v2p8_enable_count > 0)
959 			goto out; /* Still needed */
960 	}
961 
962 	if (gs->v2p8_gpio >= 0)
963 		gpio_set_value(gs->v2p8_gpio, on);
964 
965 	if (gs->v2p8_reg) {
966 		regulator_set_voltage(gs->v2p8_reg, 2900000, 2900000);
967 		if (on)
968 			ret = regulator_enable(gs->v2p8_reg);
969 		else
970 			ret = regulator_disable(gs->v2p8_reg);
971 
972 		goto out;
973 	}
974 
975 	switch (pmic_id) {
976 	case PMIC_AXP:
977 		ret = axp_regulator_set(subdev->dev, gs, ALDO1_SEL_REG,
978 					ALDO1_2P8V, ALDO1_CTRL3_REG,
979 					ALDO1_CTRL3_SHIFT, on);
980 		break;
981 	case PMIC_TI:
982 		value = on ? LDO_2P8V_ON : LDO_2P8V_OFF;
983 
984 		ret = gmin_i2c_write(subdev->dev, gs->pwm_i2c_addr,
985 				     LDO9_REG, value, 0xff);
986 		break;
987 	case PMIC_CRYSTALCOVE:
988 		if (IS_ISP2401)
989 			reg = CRYSTAL_CHT_2P8V_REG;
990 		else
991 			reg = CRYSTAL_BYT_2P8V_REG;
992 
993 		value = on ? CRYSTAL_ON : CRYSTAL_OFF;
994 
995 		ret = gmin_i2c_write(subdev->dev, gs->pwm_i2c_addr,
996 				     reg, value, 0xff);
997 		break;
998 	default:
999 		dev_err(subdev->dev, "Couldn't set power mode for v2p8\n");
1000 		ret = -EINVAL;
1001 	}
1002 
1003 out:
1004 	mutex_unlock(&gmin_regulator_mutex);
1005 	return ret;
1006 }
1007 
1008 static int gmin_acpi_pm_ctrl(struct v4l2_subdev *subdev, int on)
1009 {
1010 	int ret = 0;
1011 	struct gmin_subdev *gs = find_gmin_subdev(subdev);
1012 	struct i2c_client *client = v4l2_get_subdevdata(subdev);
1013 	struct acpi_device *adev = ACPI_COMPANION(&client->dev);
1014 
1015 	/* Use the ACPI power management to control it */
1016 	on = !!on;
1017 	if (gs->clock_on == on)
1018 		return 0;
1019 
1020 	dev_dbg(subdev->dev, "Setting power state to %s\n",
1021 		on ? "on" : "off");
1022 
1023 	if (on)
1024 		ret = acpi_device_set_power(adev,
1025 					    ACPI_STATE_D0);
1026 	else
1027 		ret = acpi_device_set_power(adev,
1028 					    ACPI_STATE_D3_COLD);
1029 
1030 	if (!ret)
1031 		gs->clock_on = on;
1032 	else
1033 		dev_err(subdev->dev, "Couldn't set power state to %s\n",
1034 			on ? "on" : "off");
1035 
1036 	return ret;
1037 }
1038 
1039 static int gmin_flisclk_ctrl(struct v4l2_subdev *subdev, int on)
1040 {
1041 	int ret = 0;
1042 	struct gmin_subdev *gs = find_gmin_subdev(subdev);
1043 	struct i2c_client *client = v4l2_get_subdevdata(subdev);
1044 
1045 	if (gs->clock_on == !!on)
1046 		return 0;
1047 
1048 	if (on) {
1049 		ret = clk_set_rate(gs->pmc_clk,
1050 				   gs->clock_src ? CLK_RATE_19_2MHZ : CLK_RATE_25_0MHZ);
1051 
1052 		if (ret)
1053 			dev_err(&client->dev, "unable to set PMC rate %d\n",
1054 				gs->clock_src);
1055 
1056 		ret = clk_prepare_enable(gs->pmc_clk);
1057 		if (ret == 0)
1058 			gs->clock_on = true;
1059 	} else {
1060 		clk_disable_unprepare(gs->pmc_clk);
1061 		gs->clock_on = false;
1062 	}
1063 
1064 	return ret;
1065 }
1066 
1067 static int camera_sensor_csi_alloc(struct v4l2_subdev *sd, u32 port, u32 lanes,
1068 				   u32 format, u32 bayer_order)
1069 {
1070 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1071 	struct camera_mipi_info *csi;
1072 
1073 	csi = kzalloc(sizeof(*csi), GFP_KERNEL);
1074 	if (!csi)
1075 		return -ENOMEM;
1076 
1077 	csi->port = port;
1078 	csi->num_lanes = lanes;
1079 	csi->input_format = format;
1080 	csi->raw_bayer_order = bayer_order;
1081 	v4l2_set_subdev_hostdata(sd, csi);
1082 	csi->metadata_format = ATOMISP_INPUT_FORMAT_EMBEDDED;
1083 	csi->metadata_effective_width = NULL;
1084 	dev_info(&client->dev,
1085 		 "camera pdata: port: %d lanes: %d order: %8.8x\n",
1086 		 port, lanes, bayer_order);
1087 
1088 	return 0;
1089 }
1090 
1091 static void camera_sensor_csi_free(struct v4l2_subdev *sd)
1092 {
1093 	struct camera_mipi_info *csi;
1094 
1095 	csi = v4l2_get_subdev_hostdata(sd);
1096 	kfree(csi);
1097 }
1098 
1099 static int gmin_csi_cfg(struct v4l2_subdev *sd, int flag)
1100 {
1101 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1102 	struct gmin_subdev *gs = find_gmin_subdev(sd);
1103 
1104 	if (!client || !gs)
1105 		return -ENODEV;
1106 
1107 	if (flag)
1108 		return camera_sensor_csi_alloc(sd, gs->csi_port, gs->csi_lanes,
1109 					       gs->csi_fmt, gs->csi_bayer);
1110 	camera_sensor_csi_free(sd);
1111 	return 0;
1112 }
1113 
1114 int atomisp_register_sensor_no_gmin(struct v4l2_subdev *subdev, u32 lanes,
1115 				    enum atomisp_input_format format,
1116 				    enum atomisp_bayer_order bayer_order)
1117 {
1118 	struct i2c_client *client = v4l2_get_subdevdata(subdev);
1119 	struct acpi_device *adev = ACPI_COMPANION(&client->dev);
1120 	int i, ret, clock_num, port = 0;
1121 
1122 	if (adev) {
1123 		/* Get ACPI _PR0 derived clock to determine the csi_port default */
1124 		if (acpi_device_power_manageable(adev)) {
1125 			clock_num = atomisp_get_acpi_power(&client->dev);
1126 
1127 			/* Compare clock to CsiPort 1 pmc-clock used in the CHT/BYT reference designs */
1128 			if (IS_ISP2401)
1129 				port = clock_num == 4 ? 1 : 0;
1130 			else
1131 				port = clock_num == 0 ? 1 : 0;
1132 		}
1133 
1134 		port = gmin_get_var_int(&client->dev, false, "CsiPort", port);
1135 		lanes = gmin_get_var_int(&client->dev, false, "CsiLanes", lanes);
1136 	}
1137 
1138 	for (i = 0; i < MAX_SUBDEVS; i++)
1139 		if (!pdata.subdevs[i].type)
1140 			break;
1141 
1142 	if (i >= MAX_SUBDEVS) {
1143 		dev_err(&client->dev, "Error too many subdevs already registered\n");
1144 		return -ENOMEM;
1145 	}
1146 
1147 	ret = camera_sensor_csi_alloc(subdev, port, lanes, format, bayer_order);
1148 	if (ret)
1149 		return ret;
1150 
1151 	pdata.subdevs[i].type = RAW_CAMERA;
1152 	pdata.subdevs[i].port = port;
1153 	pdata.subdevs[i].subdev = subdev;
1154 	return 0;
1155 }
1156 EXPORT_SYMBOL_GPL(atomisp_register_sensor_no_gmin);
1157 
1158 void atomisp_unregister_subdev(struct v4l2_subdev *subdev)
1159 {
1160 	int i;
1161 
1162 	for (i = 0; i < MAX_SUBDEVS; i++) {
1163 		if (pdata.subdevs[i].subdev != subdev)
1164 			continue;
1165 
1166 		camera_sensor_csi_free(subdev);
1167 		pdata.subdevs[i].subdev = NULL;
1168 		pdata.subdevs[i].type = 0;
1169 		pdata.subdevs[i].port = 0;
1170 		break;
1171 	}
1172 }
1173 EXPORT_SYMBOL_GPL(atomisp_unregister_subdev);
1174 
1175 static struct camera_vcm_control *gmin_get_vcm_ctrl(struct v4l2_subdev *subdev,
1176 	char *camera_module)
1177 {
1178 	struct i2c_client *client = v4l2_get_subdevdata(subdev);
1179 	struct gmin_subdev *gs = find_gmin_subdev(subdev);
1180 	struct camera_vcm_control *vcm;
1181 
1182 	if (!client || !gs)
1183 		return NULL;
1184 
1185 	if (!camera_module)
1186 		return NULL;
1187 
1188 	mutex_lock(&vcm_lock);
1189 	list_for_each_entry(vcm, &vcm_devices, list) {
1190 		if (!strcmp(camera_module, vcm->camera_module)) {
1191 			mutex_unlock(&vcm_lock);
1192 			return vcm;
1193 		}
1194 	}
1195 
1196 	mutex_unlock(&vcm_lock);
1197 	return NULL;
1198 }
1199 
1200 static struct camera_sensor_platform_data pmic_gmin_plat = {
1201 	.gpio0_ctrl = gmin_gpio0_ctrl,
1202 	.gpio1_ctrl = gmin_gpio1_ctrl,
1203 	.v1p8_ctrl = gmin_v1p8_ctrl,
1204 	.v2p8_ctrl = gmin_v2p8_ctrl,
1205 	.v1p2_ctrl = gmin_v1p2_ctrl,
1206 	.flisclk_ctrl = gmin_flisclk_ctrl,
1207 	.csi_cfg = gmin_csi_cfg,
1208 	.get_vcm_ctrl = gmin_get_vcm_ctrl,
1209 };
1210 
1211 static struct camera_sensor_platform_data acpi_gmin_plat = {
1212 	.gpio0_ctrl = gmin_gpio0_ctrl,
1213 	.gpio1_ctrl = gmin_gpio1_ctrl,
1214 	.v1p8_ctrl = gmin_acpi_pm_ctrl,
1215 	.v2p8_ctrl = gmin_acpi_pm_ctrl,
1216 	.v1p2_ctrl = gmin_acpi_pm_ctrl,
1217 	.flisclk_ctrl = gmin_acpi_pm_ctrl,
1218 	.csi_cfg = gmin_csi_cfg,
1219 	.get_vcm_ctrl = gmin_get_vcm_ctrl,
1220 };
1221 
1222 struct camera_sensor_platform_data *
1223 gmin_camera_platform_data(struct v4l2_subdev *subdev,
1224 			  enum atomisp_input_format csi_format,
1225 			  enum atomisp_bayer_order csi_bayer)
1226 {
1227 	u8 pmic_i2c_addr = gmin_detect_pmic(subdev);
1228 	struct gmin_subdev *gs;
1229 
1230 	gs = find_free_gmin_subdev_slot();
1231 	gs->subdev = subdev;
1232 	gs->csi_fmt = csi_format;
1233 	gs->csi_bayer = csi_bayer;
1234 	gs->pwm_i2c_addr = pmic_i2c_addr;
1235 
1236 	gmin_subdev_add(gs);
1237 	if (gs->pmc_clk)
1238 		return &pmic_gmin_plat;
1239 	else
1240 		return &acpi_gmin_plat;
1241 }
1242 EXPORT_SYMBOL_GPL(gmin_camera_platform_data);
1243 
1244 int atomisp_gmin_register_vcm_control(struct camera_vcm_control *vcmCtrl)
1245 {
1246 	if (!vcmCtrl)
1247 		return -EINVAL;
1248 
1249 	mutex_lock(&vcm_lock);
1250 	list_add_tail(&vcmCtrl->list, &vcm_devices);
1251 	mutex_unlock(&vcm_lock);
1252 
1253 	return 0;
1254 }
1255 EXPORT_SYMBOL_GPL(atomisp_gmin_register_vcm_control);
1256 
1257 static int gmin_get_hardcoded_var(struct device *dev,
1258 				  struct gmin_cfg_var *varlist,
1259 				  const char *var8, char *out, size_t *out_len)
1260 {
1261 	struct gmin_cfg_var *gv;
1262 
1263 	for (gv = varlist; gv->name; gv++) {
1264 		size_t vl;
1265 
1266 		if (strcmp(var8, gv->name))
1267 			continue;
1268 
1269 		dev_info(dev, "Found DMI entry for '%s'\n", var8);
1270 
1271 		vl = strlen(gv->val);
1272 		if (vl > *out_len - 1)
1273 			return -ENOSPC;
1274 
1275 		strscpy(out, gv->val, *out_len);
1276 		*out_len = vl;
1277 		return 0;
1278 	}
1279 
1280 	return -EINVAL;
1281 }
1282 
1283 
1284 static int gmin_get_config_dsm_var(struct device *dev,
1285 				   const char *var,
1286 				   char *out, size_t *out_len)
1287 {
1288 	acpi_handle handle = ACPI_HANDLE(dev);
1289 	union acpi_object *obj, *cur = NULL;
1290 	int i;
1291 
1292 	/*
1293 	 * The data reported by "CamClk" seems to be either 0 or 1 at the
1294 	 * _DSM table.
1295 	 *
1296 	 * At the ACPI tables we looked so far, this is not related to the
1297 	 * actual clock source for the sensor, which is given by the
1298 	 * _PR0 ACPI table. So, ignore it, as otherwise this will be
1299 	 * set to a wrong value.
1300 	 */
1301 	if (!strcmp(var, "CamClk"))
1302 		return -EINVAL;
1303 
1304 	/* Return on unexpected object type */
1305 	obj = acpi_evaluate_dsm_typed(handle, &atomisp_dsm_guid, 0, 0, NULL,
1306 				      ACPI_TYPE_PACKAGE);
1307 	if (!obj) {
1308 		dev_info_once(dev, "Didn't find ACPI _DSM table.\n");
1309 		return -EINVAL;
1310 	}
1311 
1312 #if 0 /* Just for debugging purposes */
1313 	for (i = 0; i < obj->package.count; i++) {
1314 		union acpi_object *cur = &obj->package.elements[i];
1315 
1316 		if (cur->type == ACPI_TYPE_INTEGER)
1317 			dev_info(dev, "object #%d, type %d, value: %lld\n",
1318 				 i, cur->type, cur->integer.value);
1319 		else if (cur->type == ACPI_TYPE_STRING)
1320 			dev_info(dev, "object #%d, type %d, string: %s\n",
1321 				 i, cur->type, cur->string.pointer);
1322 		else
1323 			dev_info(dev, "object #%d, type %d\n",
1324 				 i, cur->type);
1325 	}
1326 #endif
1327 
1328 	/* Seek for the desired var */
1329 	for (i = 0; i < obj->package.count - 1; i += 2) {
1330 		if (obj->package.elements[i].type == ACPI_TYPE_STRING &&
1331 		    !strcmp(obj->package.elements[i].string.pointer, var)) {
1332 			/* Next element should be the required value */
1333 			cur = &obj->package.elements[i + 1];
1334 			break;
1335 		}
1336 	}
1337 
1338 	if (!cur) {
1339 		dev_info(dev, "didn't found _DSM entry for '%s'\n", var);
1340 		ACPI_FREE(obj);
1341 		return -EINVAL;
1342 	}
1343 
1344 	/*
1345 	 * While it could be possible to have an ACPI_TYPE_INTEGER,
1346 	 * and read the value from cur->integer.value, the table
1347 	 * seen so far uses the string type. So, produce a warning
1348 	 * if it founds something different than string, letting it
1349 	 * to fall back to the old code.
1350 	 */
1351 	if (cur && cur->type != ACPI_TYPE_STRING) {
1352 		dev_info(dev, "found non-string _DSM entry for '%s'\n", var);
1353 		ACPI_FREE(obj);
1354 		return -EINVAL;
1355 	}
1356 
1357 	dev_info(dev, "found _DSM entry for '%s': %s\n", var,
1358 		 cur->string.pointer);
1359 	strscpy(out, cur->string.pointer, *out_len);
1360 	*out_len = strlen(cur->string.pointer);
1361 
1362 	ACPI_FREE(obj);
1363 	return 0;
1364 }
1365 
1366 /* Retrieves a device-specific configuration variable.  The dev
1367  * argument should be a device with an ACPI companion, as all
1368  * configuration is based on firmware ID.
1369  */
1370 static int gmin_get_config_var(struct device *maindev,
1371 			       bool is_gmin,
1372 			       const char *var,
1373 			       char *out, size_t *out_len)
1374 {
1375 	struct acpi_device *adev = ACPI_COMPANION(maindev);
1376 	efi_char16_t var16[CFG_VAR_NAME_MAX];
1377 	const struct dmi_system_id *id;
1378 	char var8[CFG_VAR_NAME_MAX];
1379 	efi_status_t status;
1380 	int i, ret;
1381 
1382 	if (!is_gmin && adev)
1383 		ret = snprintf(var8, sizeof(var8), "%s_%s", acpi_dev_name(adev), var);
1384 	else
1385 		ret = snprintf(var8, sizeof(var8), "gmin_%s", var);
1386 
1387 	if (ret < 0 || ret >= sizeof(var8) - 1)
1388 		return -EINVAL;
1389 
1390 	/* DMI based quirks override both the _DSM table and EFI variables */
1391 	id = dmi_first_match(gmin_vars);
1392 	if (id) {
1393 		ret = gmin_get_hardcoded_var(maindev, id->driver_data, var8,
1394 					     out, out_len);
1395 		if (!ret)
1396 			return 0;
1397 	}
1398 
1399 	/* For sensors, try first to use the _DSM table */
1400 	if (!is_gmin) {
1401 		ret = gmin_get_config_dsm_var(maindev, var, out, out_len);
1402 		if (!ret)
1403 			return 0;
1404 	}
1405 
1406 	/* Our variable names are ASCII by construction, but EFI names
1407 	 * are wide chars.  Convert and zero-pad.
1408 	 */
1409 	memset(var16, 0, sizeof(var16));
1410 	for (i = 0; i < sizeof(var8) && var8[i]; i++)
1411 		var16[i] = var8[i];
1412 
1413 	status = EFI_UNSUPPORTED;
1414 	if (efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE))
1415 		status = efi.get_variable(var16, &GMIN_CFG_VAR_EFI_GUID, NULL,
1416 					  (unsigned long *)out_len, out);
1417 	if (status == EFI_SUCCESS) {
1418 		dev_info(maindev, "found EFI entry for '%s'\n", var8);
1419 	} else if (is_gmin) {
1420 		dev_info(maindev, "Failed to find EFI gmin variable %s\n", var8);
1421 	} else {
1422 		dev_info(maindev, "Failed to find EFI variable %s\n", var8);
1423 	}
1424 
1425 	return ret;
1426 }
1427 
1428 int gmin_get_var_int(struct device *dev, bool is_gmin, const char *var, int def)
1429 {
1430 	char val[CFG_VAR_NAME_MAX];
1431 	size_t len = sizeof(val);
1432 	long result;
1433 	int ret;
1434 
1435 	ret = gmin_get_config_var(dev, is_gmin, var, val, &len);
1436 	if (!ret) {
1437 		val[len] = 0;
1438 		ret = kstrtol(val, 0, &result);
1439 	} else {
1440 		dev_info(dev, "%s: using default (%d)\n", var, def);
1441 	}
1442 
1443 	return ret ? def : result;
1444 }
1445 EXPORT_SYMBOL_GPL(gmin_get_var_int);
1446 
1447 /* PCI quirk: The BYT ISP advertises PCI runtime PM but it doesn't
1448  * work.  Disable so the kernel framework doesn't hang the device
1449  * trying.  The driver itself does direct calls to the PUNIT to manage
1450  * ISP power.
1451  */
1452 static void isp_pm_cap_fixup(struct pci_dev *pdev)
1453 {
1454 	dev_info(&pdev->dev, "Disabling PCI power management on camera ISP\n");
1455 	pdev->pm_cap = 0;
1456 }
1457 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x0f38, isp_pm_cap_fixup);
1458 
1459 MODULE_DESCRIPTION("Ancillary routines for binding ACPI devices");
1460 MODULE_LICENSE("GPL");
1461 
1462 /*
1463  * The below helper functions don't really belong here and should eventually be
1464  * moved to some place under drivers/media/v4l2-core.
1465  */
1466 #include <linux/platform_data/x86/soc.h>
1467 
1468 /*
1469  * 79234640-9e10-4fea-a5c1-b5aa8b19756f
1470  * This _DSM GUID returns information about the GPIO lines mapped to a sensor.
1471  * Function number 1 returns a count of the GPIO lines that are mapped.
1472  * Subsequent functions return 32 bit ints encoding information about the GPIO.
1473  */
1474 static const guid_t intel_sensor_gpio_info_guid =
1475 	GUID_INIT(0x79234640, 0x9e10, 0x4fea,
1476 		  0xa5, 0xc1, 0xb5, 0xaa, 0x8b, 0x19, 0x75, 0x6f);
1477 
1478 /*
1479  * 822ace8f-2814-4174-a56b-5f029fe079ee
1480  * This _DSM GUID returns a string from the sensor device, which acts as a
1481  * module identifier.
1482  */
1483 static const guid_t intel_sensor_module_guid =
1484 	GUID_INIT(0x822ace8f, 0x2814, 0x4174,
1485 		  0xa5, 0x6b, 0x5f, 0x02, 0x9f, 0xe0, 0x79, 0xee);
1486 
1487 #define INTEL_DSM_TYPE_SHIFT				0
1488 #define INTEL_DSM_TYPE_MASK				GENMASK(7, 0)
1489 #define INTEL_DSM_PIN_SHIFT				8
1490 #define INTEL_DSM_PIN_MASK				GENMASK(15, 8)
1491 #define INTEL_DSM_SENSOR_ON_VAL_SHIFT			24
1492 #define INTEL_DSM_SENSOR_ON_VAL_MASK			GENMASK(31, 24)
1493 
1494 #define INTEL_DSM_TYPE(x) \
1495 	(((x) & INTEL_DSM_TYPE_MASK) >> INTEL_DSM_TYPE_SHIFT)
1496 #define INTEL_DSM_PIN(x) \
1497 	(((x) & INTEL_DSM_PIN_MASK) >> INTEL_DSM_PIN_SHIFT)
1498 #define INTEL_DSM_SENSOR_ON_VAL(x) \
1499 	(((x) & INTEL_DSM_SENSOR_ON_VAL_MASK) >> INTEL_DSM_SENSOR_ON_VAL_SHIFT)
1500 
1501 #define V4L2_SENSOR_MAX_ACPI_GPIOS			2u
1502 
1503 struct v4l2_acpi_gpio_map {
1504 	struct acpi_gpio_params params[V4L2_SENSOR_MAX_ACPI_GPIOS];
1505 	struct acpi_gpio_mapping mapping[V4L2_SENSOR_MAX_ACPI_GPIOS + 1];
1506 };
1507 
1508 struct v4l2_acpi_gpio_parsing_data {
1509 	struct device *dev;
1510 	u32 settings[V4L2_SENSOR_MAX_ACPI_GPIOS];
1511 	unsigned int settings_count;
1512 	unsigned int res_count;
1513 	unsigned int map_count;
1514 	struct v4l2_acpi_gpio_map *map;
1515 };
1516 
1517 /* Note this always returns 1 to continue looping so that res_count is accurate */
1518 static int v4l2_acpi_handle_gpio_res(struct acpi_resource *ares, void *_data)
1519 {
1520 	struct v4l2_acpi_gpio_parsing_data *data = _data;
1521 	struct acpi_resource_gpio *agpio;
1522 	const char *name;
1523 	bool active_low;
1524 	unsigned int i;
1525 	u32 settings;
1526 	u8 pin;
1527 
1528 	if (!acpi_gpio_get_io_resource(ares, &agpio))
1529 		return 1; /* Not a GPIO, continue the loop */
1530 
1531 	data->res_count++;
1532 
1533 	pin = agpio->pin_table[0];
1534 	for (i = 0; i < data->settings_count; i++) {
1535 		if (INTEL_DSM_PIN(data->settings[i]) == pin) {
1536 			settings = data->settings[i];
1537 			break;
1538 		}
1539 	}
1540 
1541 	if (i == data->settings_count) {
1542 		dev_warn(data->dev, "Could not find DSM GPIO settings for pin %d\n", pin);
1543 		return 1;
1544 	}
1545 
1546 	switch (INTEL_DSM_TYPE(settings)) {
1547 	case 0:
1548 		name = "reset-gpios";
1549 		break;
1550 	case 1:
1551 		name = "powerdown-gpios";
1552 		break;
1553 	default:
1554 		dev_warn(data->dev, "Unknown GPIO type 0x%02lx for pin %d\n",
1555 			 INTEL_DSM_TYPE(settings), pin);
1556 		return 1;
1557 	}
1558 
1559 	/*
1560 	 * Both reset and power-down need to be logical false when the sensor
1561 	 * is on (sensor should not be in reset and not be powered-down). So
1562 	 * when the sensor-on-value (which is the physical pin value) is high,
1563 	 * then the signal is active-low.
1564 	 */
1565 	active_low = INTEL_DSM_SENSOR_ON_VAL(settings) ? true : false;
1566 
1567 	i = data->map_count;
1568 	if (i == V4L2_SENSOR_MAX_ACPI_GPIOS)
1569 		return 1;
1570 
1571 	/* res_count is already incremented */
1572 	data->map->params[i].crs_entry_index = data->res_count - 1;
1573 	data->map->params[i].active_low = active_low;
1574 	data->map->mapping[i].name = name;
1575 	data->map->mapping[i].data = &data->map->params[i];
1576 	data->map->mapping[i].size = 1;
1577 	data->map_count++;
1578 
1579 	dev_info(data->dev, "%s crs %d %s pin %d active-%s\n", name,
1580 		 data->res_count - 1, agpio->resource_source.string_ptr,
1581 		 pin, active_low ? "low" : "high");
1582 
1583 	return 1;
1584 }
1585 
1586 /*
1587  * Helper function to create an ACPI GPIO lookup table for sensor reset and
1588  * powerdown signals on Intel Bay Trail (BYT) and Cherry Trail (CHT) devices,
1589  * including setting the correct polarity for the GPIO.
1590  *
1591  * This uses the "79234640-9e10-4fea-a5c1-b5aa8b19756f" DSM method directly
1592  * on the sensor device's ACPI node. This is different from later Intel
1593  * hardware which has a separate INT3472 with this info. Since there is
1594  * no separate firmware-node to which we can bind to register the GPIO lookups
1595  * this unfortunately means that all sensor drivers which may be used on
1596  * BYT or CHT hw need to call this function. This also means that this function
1597  * may only fail when it is actually called on BYT/CHT hw. In all other cases
1598  * it must always succeed.
1599  *
1600  * Note this code uses the same DSM GUID as the INT3472 discrete.c code
1601  * and there is some overlap, but there are enough differences that it is
1602  * difficult to share the code.
1603  */
1604 int v4l2_get_acpi_sensor_info(struct device *dev, char **module_id_str)
1605 {
1606 	struct acpi_device *adev = ACPI_COMPANION(dev);
1607 	struct v4l2_acpi_gpio_parsing_data data = { };
1608 	LIST_HEAD(resource_list);
1609 	union acpi_object *obj;
1610 	unsigned int i, j;
1611 	int ret;
1612 
1613 	if (module_id_str)
1614 		*module_id_str = NULL;
1615 
1616 	if (!adev)
1617 		return 0;
1618 
1619 	obj = acpi_evaluate_dsm_typed(adev->handle, &intel_sensor_module_guid,
1620 				      0x00, 0x01, NULL, ACPI_TYPE_STRING);
1621 	if (obj) {
1622 		dev_info(dev, "Sensor module id: '%s'\n", obj->string.pointer);
1623 		if (module_id_str)
1624 			*module_id_str = kstrdup(obj->string.pointer, GFP_KERNEL);
1625 
1626 		ACPI_FREE(obj);
1627 	}
1628 
1629 	if (!soc_intel_is_byt() && !soc_intel_is_cht())
1630 		return 0;
1631 
1632 	/*
1633 	 * First get the GPIO-settings count and then get count GPIO-settings
1634 	 * values. Note the order of these may differ from the order in which
1635 	 * the GPIOs are listed on the ACPI resources! So we first store them all
1636 	 * and then enumerate the ACPI resources and match them up by pin number.
1637 	 */
1638 	obj = acpi_evaluate_dsm_typed(adev->handle,
1639 				      &intel_sensor_gpio_info_guid, 0x00, 1,
1640 				      NULL, ACPI_TYPE_INTEGER);
1641 	if (!obj)
1642 		return dev_err_probe(dev, -EIO, "No _DSM entry for GPIO pin count\n");
1643 
1644 	data.settings_count = obj->integer.value;
1645 	ACPI_FREE(obj);
1646 
1647 	if (data.settings_count > V4L2_SENSOR_MAX_ACPI_GPIOS)
1648 		return dev_err_probe(dev, -EIO, "Too many GPIOs %u > %u\n",
1649 				     data.settings_count, V4L2_SENSOR_MAX_ACPI_GPIOS);
1650 
1651 	for (i = 0; i < data.settings_count; i++) {
1652 		/*
1653 		 * i + 2 because the index of this _DSM function is 1-based
1654 		 * and the first function is just a count.
1655 		 */
1656 		obj = acpi_evaluate_dsm_typed(adev->handle,
1657 					      &intel_sensor_gpio_info_guid,
1658 					      0x00, i + 2,
1659 					      NULL, ACPI_TYPE_INTEGER);
1660 		if (!obj)
1661 			return dev_err_probe(dev, -EIO, "No _DSM entry for GPIO pin %u\n", i);
1662 
1663 		data.settings[i] = obj->integer.value;
1664 		ACPI_FREE(obj);
1665 	}
1666 
1667 	/* Since we match up by pin-number the pin-numbers must be unique */
1668 	for (i = 0; i < data.settings_count; i++) {
1669 		for (j = i + 1; j < data.settings_count; j++) {
1670 			if (INTEL_DSM_PIN(data.settings[i]) !=
1671 			    INTEL_DSM_PIN(data.settings[j]))
1672 				continue;
1673 
1674 			return dev_err_probe(dev, -EIO, "Duplicate pin number %lu\n",
1675 					     INTEL_DSM_PIN(data.settings[i]));
1676 		}
1677 	}
1678 
1679 	/* Use devm_kzalloc() for the mappings + params to auto-free them */
1680 	data.map = devm_kzalloc(dev, sizeof(*data.map), GFP_KERNEL);
1681 	if (!data.map)
1682 		return -ENOMEM;
1683 
1684 	/* Now parse the ACPI resources and build the lookup table */
1685 	data.dev = dev;
1686 	ret = acpi_dev_get_resources(adev, &resource_list,
1687 				     v4l2_acpi_handle_gpio_res, &data);
1688 	if (ret < 0)
1689 		return ret;
1690 
1691 	acpi_dev_free_resource_list(&resource_list);
1692 
1693 	if (data.map_count != data.settings_count ||
1694 	    data.res_count != data.settings_count)
1695 		dev_warn(dev, "ACPI GPIO resources vs DSM GPIO-info count mismatch (dsm: %d res: %d map %d\n",
1696 			 data.settings_count, data.res_count, data.map_count);
1697 
1698 	return devm_acpi_dev_add_driver_gpios(dev, data.map->mapping);
1699 }
1700 EXPORT_SYMBOL_GPL(v4l2_get_acpi_sensor_info);
1701