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