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