1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Code to build software firmware node graph for atomisp2 connected sensors
4 * from ACPI tables.
5 *
6 * Copyright (C) 2023 Hans de Goede <hdegoede@redhat.com>
7 *
8 * Based on drivers/media/pci/intel/ipu3/cio2-bridge.c written by:
9 * Dan Scally <djrscally@gmail.com>
10 */
11
12 #include <linux/acpi.h>
13 #include <linux/clk.h>
14 #include <linux/device.h>
15 #include <linux/dmi.h>
16 #include <linux/platform_data/x86/int3472.h>
17 #include <linux/property.h>
18
19 #include <media/ipu-bridge.h>
20 #include <media/v4l2-fwnode.h>
21
22 #include "atomisp_cmd.h"
23 #include "atomisp_csi2.h"
24 #include "atomisp_internal.h"
25
26 #define PMC_CLK_RATE_19_2MHZ 19200000
27
28 /*
29 * dc2f6c4f-045b-4f1d-97b9-882a6860a4be
30 * This _DSM GUID returns a package with n*2 strings, with each set of 2 strings
31 * forming a key, value pair for settings like e.g. "CsiLanes" = "1".
32 */
33 static const guid_t atomisp_dsm_guid =
34 GUID_INIT(0xdc2f6c4f, 0x045b, 0x4f1d,
35 0x97, 0xb9, 0x88, 0x2a, 0x68, 0x60, 0xa4, 0xbe);
36
37 /*
38 * 75c9a639-5c8a-4a00-9f48-a9c3b5da789f
39 * This _DSM GUID returns a string giving the VCM type e.g. "AD5823".
40 */
41 static const guid_t vcm_dsm_guid =
42 GUID_INIT(0x75c9a639, 0x5c8a, 0x4a00,
43 0x9f, 0x48, 0xa9, 0xc3, 0xb5, 0xda, 0x78, 0x9f);
44
45 struct atomisp_sensor_config {
46 int lanes;
47 bool vcm;
48 };
49
50 #define ATOMISP_SENSOR_CONFIG(_HID, _LANES, _VCM) \
51 { \
52 .id = _HID, \
53 .driver_data = (long)&((const struct atomisp_sensor_config) { \
54 .lanes = _LANES, \
55 .vcm = _VCM, \
56 }) \
57 }
58
59 /*
60 * gmin_cfg parsing code. This is a cleaned up version of the gmin_cfg parsing
61 * code from atomisp_gmin_platform.c.
62 * Once all sensors are moved to v4l2-async probing atomisp_gmin_platform.c can
63 * be removed and the duplication of this code goes away.
64 */
65 struct gmin_cfg_var {
66 const char *acpi_dev_name;
67 const char *key;
68 const char *val;
69 };
70
71 static struct gmin_cfg_var lenovo_ideapad_miix_310_vars[] = {
72 /* _DSM contains the wrong CsiPort! */
73 { "OVTI2680:01", "CsiPort", "0" },
74 {}
75 };
76
77 static struct gmin_cfg_var xiaomi_mipad2_vars[] = {
78 /* _DSM contains the wrong CsiPort for the front facing OV5693 sensor */
79 { "INT33BE:00", "CsiPort", "0" },
80 /* _DSM contains the wrong CsiLanes for the back facing T4KA3 sensor */
81 { "XMCC0003:00", "CsiLanes", "4" },
82 {}
83 };
84
85 static const struct dmi_system_id gmin_cfg_dmi_overrides[] = {
86 {
87 /* Lenovo Ideapad Miix 310 */
88 .matches = {
89 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
90 DMI_MATCH(DMI_PRODUCT_VERSION, "MIIX 310-10"),
91 },
92 .driver_data = lenovo_ideapad_miix_310_vars,
93 },
94 {
95 /* Xiaomi Mipad2 */
96 .matches = {
97 DMI_MATCH(DMI_SYS_VENDOR, "Xiaomi Inc"),
98 DMI_MATCH(DMI_PRODUCT_NAME, "Mipad2"),
99 },
100 .driver_data = xiaomi_mipad2_vars,
101 },
102 {}
103 };
104
gmin_cfg_get_dsm(struct acpi_device * adev,const char * key)105 static char *gmin_cfg_get_dsm(struct acpi_device *adev, const char *key)
106 {
107 union acpi_object *obj, *key_el, *val_el;
108 char *val = NULL;
109 int i;
110
111 obj = acpi_evaluate_dsm_typed(adev->handle, &atomisp_dsm_guid, 0, 0,
112 NULL, ACPI_TYPE_PACKAGE);
113 if (!obj)
114 return NULL;
115
116 for (i = 0; i < obj->package.count - 1; i += 2) {
117 key_el = &obj->package.elements[i + 0];
118 val_el = &obj->package.elements[i + 1];
119
120 if (key_el->type != ACPI_TYPE_STRING || val_el->type != ACPI_TYPE_STRING)
121 break;
122
123 if (!strcmp(key_el->string.pointer, key)) {
124 val = kstrdup(val_el->string.pointer, GFP_KERNEL);
125 if (!val)
126 break;
127
128 acpi_handle_info(adev->handle, "%s: Using DSM entry %s=%s\n",
129 dev_name(&adev->dev), key, val);
130 break;
131 }
132 }
133
134 ACPI_FREE(obj);
135 return val;
136 }
137
gmin_cfg_get_dmi_override(struct acpi_device * adev,const char * key)138 static char *gmin_cfg_get_dmi_override(struct acpi_device *adev, const char *key)
139 {
140 const struct dmi_system_id *id;
141 struct gmin_cfg_var *gv;
142
143 id = dmi_first_match(gmin_cfg_dmi_overrides);
144 if (!id)
145 return NULL;
146
147 for (gv = id->driver_data; gv->acpi_dev_name; gv++) {
148 if (strcmp(gv->acpi_dev_name, acpi_dev_name(adev)))
149 continue;
150
151 if (strcmp(key, gv->key))
152 continue;
153
154 acpi_handle_info(adev->handle, "%s: Using DMI entry %s=%s\n",
155 dev_name(&adev->dev), key, gv->val);
156 return kstrdup(gv->val, GFP_KERNEL);
157 }
158
159 return NULL;
160 }
161
gmin_cfg_get(struct acpi_device * adev,const char * key)162 static char *gmin_cfg_get(struct acpi_device *adev, const char *key)
163 {
164 char *val;
165
166 val = gmin_cfg_get_dmi_override(adev, key);
167 if (val)
168 return val;
169
170 return gmin_cfg_get_dsm(adev, key);
171 }
172
gmin_cfg_get_int(struct acpi_device * adev,const char * key,int default_val)173 static int gmin_cfg_get_int(struct acpi_device *adev, const char *key, int default_val)
174 {
175 char *str_val;
176 long int_val;
177 int ret;
178
179 str_val = gmin_cfg_get(adev, key);
180 if (!str_val)
181 goto out_use_default;
182
183 ret = kstrtoul(str_val, 0, &int_val);
184 kfree(str_val);
185 if (ret)
186 goto out_use_default;
187
188 return int_val;
189
190 out_use_default:
191 acpi_handle_info(adev->handle, "%s: Using default %s=%d\n",
192 dev_name(&adev->dev), key, default_val);
193 return default_val;
194 }
195
atomisp_csi2_get_pmc_clk_nr_from_acpi_pr0(struct acpi_device * adev)196 static int atomisp_csi2_get_pmc_clk_nr_from_acpi_pr0(struct acpi_device *adev)
197 {
198 /* ACPI_PATH_SEGMENT_LENGTH is guaranteed to be big enough for name + 0 term. */
199 char name[ACPI_PATH_SEGMENT_LENGTH];
200 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
201 struct acpi_buffer b_name = { sizeof(name), name };
202 union acpi_object *package, *element;
203 int i, ret = -ENOENT;
204 acpi_handle rhandle;
205 acpi_status status;
206 u8 clock_num;
207
208 status = acpi_evaluate_object_typed(adev->handle, "_PR0", NULL, &buffer, ACPI_TYPE_PACKAGE);
209 if (ACPI_FAILURE(status))
210 return -ENOENT;
211
212 package = buffer.pointer;
213 for (i = 0; i < package->package.count; i++) {
214 element = &package->package.elements[i];
215
216 if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
217 continue;
218
219 rhandle = element->reference.handle;
220 if (!rhandle)
221 continue;
222
223 acpi_get_name(rhandle, ACPI_SINGLE_NAME, &b_name);
224
225 if (str_has_prefix(name, "CLK") && !kstrtou8(&name[3], 10, &clock_num) &&
226 clock_num <= 4) {
227 ret = clock_num;
228 break;
229 }
230 }
231
232 ACPI_FREE(buffer.pointer);
233
234 if (ret < 0)
235 acpi_handle_warn(adev->handle, "%s: Could not find PMC clk in _PR0\n",
236 dev_name(&adev->dev));
237
238 return ret;
239 }
240
atomisp_csi2_set_pmc_clk_freq(struct acpi_device * adev,int clock_num)241 static int atomisp_csi2_set_pmc_clk_freq(struct acpi_device *adev, int clock_num)
242 {
243 struct clk *clk;
244 char name[14];
245 int ret;
246
247 if (clock_num < 0)
248 return 0;
249
250 snprintf(name, sizeof(name), "pmc_plt_clk_%d", clock_num);
251
252 clk = clk_get(NULL, name);
253 if (IS_ERR(clk)) {
254 ret = PTR_ERR(clk);
255 acpi_handle_err(adev->handle, "%s: Error getting clk %s: %d\n",
256 dev_name(&adev->dev), name, ret);
257 return ret;
258 }
259
260 /*
261 * The firmware might enable the clock at boot, to change
262 * the rate we must ensure the clock is disabled.
263 */
264 ret = clk_prepare_enable(clk);
265 if (!ret)
266 clk_disable_unprepare(clk);
267 if (!ret)
268 ret = clk_set_rate(clk, PMC_CLK_RATE_19_2MHZ);
269 if (ret)
270 acpi_handle_err(adev->handle, "%s: Error setting clk-rate for %s: %d\n",
271 dev_name(&adev->dev), name, ret);
272
273 clk_put(clk);
274 return ret;
275 }
276
atomisp_csi2_get_port(struct acpi_device * adev,int clock_num)277 static int atomisp_csi2_get_port(struct acpi_device *adev, int clock_num)
278 {
279 int port;
280
281 /*
282 * Compare clock-number to the PMC-clock used for CsiPort 1
283 * in the CHT/BYT reference designs.
284 */
285 if (IS_ISP2401)
286 port = clock_num == 4 ? 1 : 0;
287 else
288 port = clock_num == 0 ? 1 : 0;
289
290 /* Intel DSM or DMI quirk overrides _PR0 CLK derived default */
291 return gmin_cfg_get_int(adev, "CsiPort", port);
292 }
293
294 /*
295 * Alloc and fill an int3472_discrete_device struct so that we can re-use
296 * the INT3472 sensor GPIO mapping code.
297 *
298 * This gets called from ipu_bridge_init() which runs only once per boot,
299 * the created faux int3472 device is leaked on purpose to keep the created
300 * GPIO mappings around permanently.
301 */
atomisp_csi2_add_gpio_mappings(struct acpi_device * adev)302 static int atomisp_csi2_add_gpio_mappings(struct acpi_device *adev)
303 {
304 struct int3472_discrete_device *int3472;
305 int ret;
306
307 /* Max num GPIOs we've seen plus a terminator */
308 int3472 = kzalloc(struct_size(int3472, gpios.table, INT3472_MAX_SENSOR_GPIOS + 1),
309 GFP_KERNEL);
310 if (!int3472)
311 return -ENOMEM;
312
313 /*
314 * On atomisp the _DSM to get the GPIO type must be made on the sensor
315 * adev, rather than on a separate INT3472 adev.
316 */
317 int3472->adev = adev;
318 int3472->dev = &adev->dev;
319 int3472->sensor = adev;
320
321 int3472->sensor_name = kasprintf(GFP_KERNEL, I2C_DEV_NAME_FORMAT, acpi_dev_name(adev));
322 if (!int3472->sensor_name) {
323 kfree(int3472);
324 return -ENOMEM;
325 }
326
327 ret = int3472_discrete_parse_crs(int3472);
328 if (ret) {
329 int3472_discrete_cleanup(int3472);
330 kfree(int3472);
331 }
332
333 return ret;
334 }
335
atomisp_csi2_get_vcm_type(struct acpi_device * adev)336 static char *atomisp_csi2_get_vcm_type(struct acpi_device *adev)
337 {
338 union acpi_object *obj;
339 char *vcm_type;
340
341 obj = acpi_evaluate_dsm_typed(adev->handle, &vcm_dsm_guid, 0, 0,
342 NULL, ACPI_TYPE_STRING);
343 if (!obj)
344 return NULL;
345
346 vcm_type = kstrdup(obj->string.pointer, GFP_KERNEL);
347 ACPI_FREE(obj);
348
349 if (!vcm_type)
350 return NULL;
351
352 string_lower(vcm_type, vcm_type);
353 return vcm_type;
354 }
355
356 static const struct acpi_device_id atomisp_sensor_configs[] = {
357 /*
358 * FIXME ov5693 modules have a VCM, but for unknown reasons
359 * the sensor fails to start streaming when instantiating
360 * an i2c-client for the VCM, so it is disabled for now.
361 */
362 ATOMISP_SENSOR_CONFIG("INT33BE", 2, false), /* OV5693 */
363 {}
364 };
365
atomisp_csi2_parse_sensor_fwnode(struct acpi_device * adev,struct ipu_sensor * sensor)366 static int atomisp_csi2_parse_sensor_fwnode(struct acpi_device *adev,
367 struct ipu_sensor *sensor)
368 {
369 const struct acpi_device_id *id;
370 int ret, clock_num;
371 bool vcm = false;
372 int lanes = 1;
373
374 id = acpi_match_acpi_device(atomisp_sensor_configs, adev);
375 if (id) {
376 struct atomisp_sensor_config *cfg =
377 (struct atomisp_sensor_config *)id->driver_data;
378
379 lanes = cfg->lanes;
380 vcm = cfg->vcm;
381 }
382
383 /*
384 * ACPI takes care of turning the PMC clock on and off, but on BYT
385 * the clock defaults to 25 MHz instead of the expected 19.2 MHz.
386 * Get the PMC-clock number from ACPI PR0 method and set it to 19.2 MHz.
387 * The PMC-clock number is also used to determine the default CSI port.
388 */
389 clock_num = atomisp_csi2_get_pmc_clk_nr_from_acpi_pr0(adev);
390
391 ret = atomisp_csi2_set_pmc_clk_freq(adev, clock_num);
392 if (ret)
393 return ret;
394
395 sensor->link = atomisp_csi2_get_port(adev, clock_num);
396 if (sensor->link >= ATOMISP_CAMERA_NR_PORTS) {
397 acpi_handle_err(adev->handle, "%s: Invalid port: %u\n",
398 dev_name(&adev->dev), sensor->link);
399 return -EINVAL;
400 }
401
402 sensor->lanes = gmin_cfg_get_int(adev, "CsiLanes", lanes);
403 if (sensor->lanes > IPU_MAX_LANES) {
404 acpi_handle_err(adev->handle, "%s: Invalid lane-count: %d\n",
405 dev_name(&adev->dev), sensor->lanes);
406 return -EINVAL;
407 }
408
409 ret = atomisp_csi2_add_gpio_mappings(adev);
410 if (ret)
411 return ret;
412
413 sensor->mclkspeed = PMC_CLK_RATE_19_2MHZ;
414 sensor->rotation = 0;
415 sensor->orientation = (sensor->link == 1) ?
416 V4L2_FWNODE_ORIENTATION_BACK : V4L2_FWNODE_ORIENTATION_FRONT;
417
418 if (vcm)
419 sensor->vcm_type = atomisp_csi2_get_vcm_type(adev);
420
421 return 0;
422 }
423
atomisp_csi2_bridge_init(struct atomisp_device * isp)424 int atomisp_csi2_bridge_init(struct atomisp_device *isp)
425 {
426 struct device *dev = isp->dev;
427 struct fwnode_handle *fwnode;
428
429 /*
430 * This function is intended to run only once and then leave
431 * the created nodes attached even after a rmmod, therefore:
432 * 1. The bridge memory is leaked deliberately on success
433 * 2. If a secondary fwnode is already set exit early.
434 */
435 fwnode = dev_fwnode(dev);
436 if (fwnode && fwnode->secondary)
437 return 0;
438
439 return ipu_bridge_init(dev, atomisp_csi2_parse_sensor_fwnode);
440 }
441
442 /******* V4L2 sub-device asynchronous registration callbacks***********/
443
444 struct sensor_async_subdev {
445 struct v4l2_async_connection asd;
446 int port;
447 };
448
449 #define to_sensor_asd(a) container_of(a, struct sensor_async_subdev, asd)
450 #define notifier_to_atomisp(n) container_of(n, struct atomisp_device, notifier)
451
452 /* .bound() notifier callback when a match is found */
atomisp_notifier_bound(struct v4l2_async_notifier * notifier,struct v4l2_subdev * sd,struct v4l2_async_connection * asd)453 static int atomisp_notifier_bound(struct v4l2_async_notifier *notifier,
454 struct v4l2_subdev *sd,
455 struct v4l2_async_connection *asd)
456 {
457 struct atomisp_device *isp = notifier_to_atomisp(notifier);
458 struct sensor_async_subdev *s_asd = to_sensor_asd(asd);
459 int ret;
460
461 if (s_asd->port >= ATOMISP_CAMERA_NR_PORTS) {
462 dev_err(isp->dev, "port %d not supported\n", s_asd->port);
463 return -EINVAL;
464 }
465
466 if (isp->sensor_subdevs[s_asd->port]) {
467 dev_err(isp->dev, "port %d already has a sensor attached\n", s_asd->port);
468 return -EBUSY;
469 }
470
471 ret = ipu_bridge_instantiate_vcm(sd->dev);
472 if (ret)
473 return ret;
474
475 isp->sensor_subdevs[s_asd->port] = sd;
476 return 0;
477 }
478
479 /* The .unbind callback */
atomisp_notifier_unbind(struct v4l2_async_notifier * notifier,struct v4l2_subdev * sd,struct v4l2_async_connection * asd)480 static void atomisp_notifier_unbind(struct v4l2_async_notifier *notifier,
481 struct v4l2_subdev *sd,
482 struct v4l2_async_connection *asd)
483 {
484 struct atomisp_device *isp = notifier_to_atomisp(notifier);
485 struct sensor_async_subdev *s_asd = to_sensor_asd(asd);
486
487 isp->sensor_subdevs[s_asd->port] = NULL;
488 }
489
490 /* .complete() is called after all subdevices have been located */
atomisp_notifier_complete(struct v4l2_async_notifier * notifier)491 static int atomisp_notifier_complete(struct v4l2_async_notifier *notifier)
492 {
493 struct atomisp_device *isp = notifier_to_atomisp(notifier);
494
495 return atomisp_register_device_nodes(isp);
496 }
497
498 static const struct v4l2_async_notifier_operations atomisp_async_ops = {
499 .bound = atomisp_notifier_bound,
500 .unbind = atomisp_notifier_unbind,
501 .complete = atomisp_notifier_complete,
502 };
503
atomisp_csi2_bridge_parse_firmware(struct atomisp_device * isp)504 int atomisp_csi2_bridge_parse_firmware(struct atomisp_device *isp)
505 {
506 int i, mipi_port, ret;
507
508 v4l2_async_nf_init(&isp->notifier, &isp->v4l2_dev);
509 isp->notifier.ops = &atomisp_async_ops;
510
511 for (i = 0; i < ATOMISP_CAMERA_NR_PORTS; i++) {
512 struct v4l2_fwnode_endpoint vep = {
513 .bus_type = V4L2_MBUS_CSI2_DPHY,
514 };
515 struct sensor_async_subdev *s_asd;
516 struct fwnode_handle *ep;
517
518 ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(isp->dev), i, 0,
519 FWNODE_GRAPH_ENDPOINT_NEXT);
520 if (!ep)
521 continue;
522
523 ret = v4l2_fwnode_endpoint_parse(ep, &vep);
524 if (ret)
525 goto err_parse;
526
527 if (vep.base.port >= ATOMISP_CAMERA_NR_PORTS) {
528 dev_err(isp->dev, "port %d not supported\n", vep.base.port);
529 ret = -EINVAL;
530 goto err_parse;
531 }
532
533 mipi_port = atomisp_port_to_mipi_port(isp, vep.base.port);
534 isp->sensor_lanes[mipi_port] = vep.bus.mipi_csi2.num_data_lanes;
535
536 s_asd = v4l2_async_nf_add_fwnode_remote(&isp->notifier, ep,
537 struct sensor_async_subdev);
538 if (IS_ERR(s_asd)) {
539 ret = PTR_ERR(s_asd);
540 goto err_parse;
541 }
542
543 s_asd->port = vep.base.port;
544
545 fwnode_handle_put(ep);
546 continue;
547
548 err_parse:
549 fwnode_handle_put(ep);
550 return ret;
551 }
552
553 return 0;
554 }
555