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