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