forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-11 072de836f53be56a70cecf70b43ae43b7ce17376
kernel/drivers/thermal/hisi_thermal.c
....@@ -55,25 +55,39 @@
5555 #define HI3660_TEMP_STEP (205)
5656 #define HI3660_TEMP_LAG (4000)
5757
58
-#define HI6220_DEFAULT_SENSOR 2
59
-#define HI3660_DEFAULT_SENSOR 1
58
+#define HI6220_CLUSTER0_SENSOR 2
59
+#define HI6220_CLUSTER1_SENSOR 1
60
+
61
+#define HI3660_LITTLE_SENSOR 0
62
+#define HI3660_BIG_SENSOR 1
63
+#define HI3660_G3D_SENSOR 2
64
+#define HI3660_MODEM_SENSOR 3
65
+
66
+struct hisi_thermal_data;
6067
6168 struct hisi_thermal_sensor {
69
+ struct hisi_thermal_data *data;
6270 struct thermal_zone_device *tzd;
71
+ const char *irq_name;
6372 uint32_t id;
6473 uint32_t thres_temp;
6574 };
6675
76
+struct hisi_thermal_ops {
77
+ int (*get_temp)(struct hisi_thermal_sensor *sensor);
78
+ int (*enable_sensor)(struct hisi_thermal_sensor *sensor);
79
+ int (*disable_sensor)(struct hisi_thermal_sensor *sensor);
80
+ int (*irq_handler)(struct hisi_thermal_sensor *sensor);
81
+ int (*probe)(struct hisi_thermal_data *data);
82
+};
83
+
6784 struct hisi_thermal_data {
68
- int (*get_temp)(struct hisi_thermal_data *data);
69
- int (*enable_sensor)(struct hisi_thermal_data *data);
70
- int (*disable_sensor)(struct hisi_thermal_data *data);
71
- int (*irq_handler)(struct hisi_thermal_data *data);
85
+ const struct hisi_thermal_ops *ops;
86
+ struct hisi_thermal_sensor *sensor;
7287 struct platform_device *pdev;
7388 struct clk *clk;
74
- struct hisi_thermal_sensor sensor;
7589 void __iomem *regs;
76
- int irq;
90
+ int nr_sensors;
7791 };
7892
7993 /*
....@@ -266,30 +280,40 @@
266280 (value << 4), addr + HI6220_TEMP0_CFG);
267281 }
268282
269
-static int hi6220_thermal_irq_handler(struct hisi_thermal_data *data)
283
+static int hi6220_thermal_irq_handler(struct hisi_thermal_sensor *sensor)
270284 {
285
+ struct hisi_thermal_data *data = sensor->data;
286
+
271287 hi6220_thermal_alarm_clear(data->regs, 1);
272288 return 0;
273289 }
274290
275
-static int hi3660_thermal_irq_handler(struct hisi_thermal_data *data)
291
+static int hi3660_thermal_irq_handler(struct hisi_thermal_sensor *sensor)
276292 {
277
- hi3660_thermal_alarm_clear(data->regs, data->sensor.id, 1);
293
+ struct hisi_thermal_data *data = sensor->data;
294
+
295
+ hi3660_thermal_alarm_clear(data->regs, sensor->id, 1);
278296 return 0;
279297 }
280298
281
-static int hi6220_thermal_get_temp(struct hisi_thermal_data *data)
299
+static int hi6220_thermal_get_temp(struct hisi_thermal_sensor *sensor)
282300 {
301
+ struct hisi_thermal_data *data = sensor->data;
302
+
283303 return hi6220_thermal_get_temperature(data->regs);
284304 }
285305
286
-static int hi3660_thermal_get_temp(struct hisi_thermal_data *data)
306
+static int hi3660_thermal_get_temp(struct hisi_thermal_sensor *sensor)
287307 {
288
- return hi3660_thermal_get_temperature(data->regs, data->sensor.id);
308
+ struct hisi_thermal_data *data = sensor->data;
309
+
310
+ return hi3660_thermal_get_temperature(data->regs, sensor->id);
289311 }
290312
291
-static int hi6220_thermal_disable_sensor(struct hisi_thermal_data *data)
313
+static int hi6220_thermal_disable_sensor(struct hisi_thermal_sensor *sensor)
292314 {
315
+ struct hisi_thermal_data *data = sensor->data;
316
+
293317 /* disable sensor module */
294318 hi6220_thermal_enable(data->regs, 0);
295319 hi6220_thermal_alarm_enable(data->regs, 0);
....@@ -300,16 +324,18 @@
300324 return 0;
301325 }
302326
303
-static int hi3660_thermal_disable_sensor(struct hisi_thermal_data *data)
327
+static int hi3660_thermal_disable_sensor(struct hisi_thermal_sensor *sensor)
304328 {
329
+ struct hisi_thermal_data *data = sensor->data;
330
+
305331 /* disable sensor module */
306
- hi3660_thermal_alarm_enable(data->regs, data->sensor.id, 0);
332
+ hi3660_thermal_alarm_enable(data->regs, sensor->id, 0);
307333 return 0;
308334 }
309335
310
-static int hi6220_thermal_enable_sensor(struct hisi_thermal_data *data)
336
+static int hi6220_thermal_enable_sensor(struct hisi_thermal_sensor *sensor)
311337 {
312
- struct hisi_thermal_sensor *sensor = &data->sensor;
338
+ struct hisi_thermal_data *data = sensor->data;
313339 int ret;
314340
315341 /* enable clock for tsensor */
....@@ -345,10 +371,10 @@
345371 return 0;
346372 }
347373
348
-static int hi3660_thermal_enable_sensor(struct hisi_thermal_data *data)
374
+static int hi3660_thermal_enable_sensor(struct hisi_thermal_sensor *sensor)
349375 {
350376 unsigned int value;
351
- struct hisi_thermal_sensor *sensor = &data->sensor;
377
+ struct hisi_thermal_data *data = sensor->data;
352378
353379 /* disable interrupt */
354380 hi3660_thermal_alarm_enable(data->regs, sensor->id, 0);
....@@ -371,20 +397,7 @@
371397 {
372398 struct platform_device *pdev = data->pdev;
373399 struct device *dev = &pdev->dev;
374
- struct resource *res;
375400 int ret;
376
-
377
- data->get_temp = hi6220_thermal_get_temp;
378
- data->enable_sensor = hi6220_thermal_enable_sensor;
379
- data->disable_sensor = hi6220_thermal_disable_sensor;
380
- data->irq_handler = hi6220_thermal_irq_handler;
381
-
382
- res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
383
- data->regs = devm_ioremap_resource(dev, res);
384
- if (IS_ERR(data->regs)) {
385
- dev_err(dev, "failed to get io address\n");
386
- return PTR_ERR(data->regs);
387
- }
388401
389402 data->clk = devm_clk_get(dev, "thermal_clk");
390403 if (IS_ERR(data->clk)) {
....@@ -394,11 +407,14 @@
394407 return ret;
395408 }
396409
397
- data->irq = platform_get_irq(pdev, 0);
398
- if (data->irq < 0)
399
- return data->irq;
410
+ data->sensor = devm_kzalloc(dev, sizeof(*data->sensor), GFP_KERNEL);
411
+ if (!data->sensor)
412
+ return -ENOMEM;
400413
401
- data->sensor.id = HI6220_DEFAULT_SENSOR;
414
+ data->sensor[0].id = HI6220_CLUSTER0_SENSOR;
415
+ data->sensor[0].irq_name = "tsensor_intr";
416
+ data->sensor[0].data = data;
417
+ data->nr_sensors = 1;
402418
403419 return 0;
404420 }
....@@ -407,38 +423,34 @@
407423 {
408424 struct platform_device *pdev = data->pdev;
409425 struct device *dev = &pdev->dev;
410
- struct resource *res;
411426
412
- data->get_temp = hi3660_thermal_get_temp;
413
- data->enable_sensor = hi3660_thermal_enable_sensor;
414
- data->disable_sensor = hi3660_thermal_disable_sensor;
415
- data->irq_handler = hi3660_thermal_irq_handler;
427
+ data->nr_sensors = 1;
416428
417
- res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
418
- data->regs = devm_ioremap_resource(dev, res);
419
- if (IS_ERR(data->regs)) {
420
- dev_err(dev, "failed to get io address\n");
421
- return PTR_ERR(data->regs);
422
- }
429
+ data->sensor = devm_kzalloc(dev, sizeof(*data->sensor) *
430
+ data->nr_sensors, GFP_KERNEL);
431
+ if (!data->sensor)
432
+ return -ENOMEM;
423433
424
- data->irq = platform_get_irq(pdev, 0);
425
- if (data->irq < 0)
426
- return data->irq;
434
+ data->sensor[0].id = HI3660_BIG_SENSOR;
435
+ data->sensor[0].irq_name = "tsensor_a73";
436
+ data->sensor[0].data = data;
427437
428
- data->sensor.id = HI3660_DEFAULT_SENSOR;
438
+ data->sensor[1].id = HI3660_LITTLE_SENSOR;
439
+ data->sensor[1].irq_name = "tsensor_a53";
440
+ data->sensor[1].data = data;
429441
430442 return 0;
431443 }
432444
433445 static int hisi_thermal_get_temp(void *__data, int *temp)
434446 {
435
- struct hisi_thermal_data *data = __data;
436
- struct hisi_thermal_sensor *sensor = &data->sensor;
447
+ struct hisi_thermal_sensor *sensor = __data;
448
+ struct hisi_thermal_data *data = sensor->data;
437449
438
- *temp = data->get_temp(data);
450
+ *temp = data->ops->get_temp(sensor);
439451
440
- dev_dbg(&data->pdev->dev, "id=%d, temp=%d, thres=%d\n",
441
- sensor->id, *temp, sensor->thres_temp);
452
+ dev_dbg(&data->pdev->dev, "tzd=%p, id=%d, temp=%d, thres=%d\n",
453
+ sensor->tzd, sensor->id, *temp, sensor->thres_temp);
442454
443455 return 0;
444456 }
....@@ -449,38 +461,39 @@
449461
450462 static irqreturn_t hisi_thermal_alarm_irq_thread(int irq, void *dev)
451463 {
452
- struct hisi_thermal_data *data = dev;
453
- struct hisi_thermal_sensor *sensor = &data->sensor;
464
+ struct hisi_thermal_sensor *sensor = dev;
465
+ struct hisi_thermal_data *data = sensor->data;
454466 int temp = 0;
455467
456
- data->irq_handler(data);
468
+ data->ops->irq_handler(sensor);
457469
458
- hisi_thermal_get_temp(data, &temp);
470
+ hisi_thermal_get_temp(sensor, &temp);
459471
460472 if (temp >= sensor->thres_temp) {
461
- dev_crit(&data->pdev->dev, "THERMAL ALARM: %d > %d\n",
462
- temp, sensor->thres_temp);
473
+ dev_crit(&data->pdev->dev,
474
+ "sensor <%d> THERMAL ALARM: %d > %d\n",
475
+ sensor->id, temp, sensor->thres_temp);
463476
464
- thermal_zone_device_update(data->sensor.tzd,
477
+ thermal_zone_device_update(sensor->tzd,
465478 THERMAL_EVENT_UNSPECIFIED);
466479
467480 } else {
468
- dev_crit(&data->pdev->dev, "THERMAL ALARM stopped: %d < %d\n",
469
- temp, sensor->thres_temp);
481
+ dev_crit(&data->pdev->dev,
482
+ "sensor <%d> THERMAL ALARM stopped: %d < %d\n",
483
+ sensor->id, temp, sensor->thres_temp);
470484 }
471485
472486 return IRQ_HANDLED;
473487 }
474488
475489 static int hisi_thermal_register_sensor(struct platform_device *pdev,
476
- struct hisi_thermal_data *data,
477490 struct hisi_thermal_sensor *sensor)
478491 {
479492 int ret, i;
480493 const struct thermal_trip *trip;
481494
482495 sensor->tzd = devm_thermal_zone_of_sensor_register(&pdev->dev,
483
- sensor->id, data,
496
+ sensor->id, sensor,
484497 &hisi_of_thermal_ops);
485498 if (IS_ERR(sensor->tzd)) {
486499 ret = PTR_ERR(sensor->tzd);
....@@ -502,14 +515,30 @@
502515 return 0;
503516 }
504517
518
+static const struct hisi_thermal_ops hi6220_ops = {
519
+ .get_temp = hi6220_thermal_get_temp,
520
+ .enable_sensor = hi6220_thermal_enable_sensor,
521
+ .disable_sensor = hi6220_thermal_disable_sensor,
522
+ .irq_handler = hi6220_thermal_irq_handler,
523
+ .probe = hi6220_thermal_probe,
524
+};
525
+
526
+static const struct hisi_thermal_ops hi3660_ops = {
527
+ .get_temp = hi3660_thermal_get_temp,
528
+ .enable_sensor = hi3660_thermal_enable_sensor,
529
+ .disable_sensor = hi3660_thermal_disable_sensor,
530
+ .irq_handler = hi3660_thermal_irq_handler,
531
+ .probe = hi3660_thermal_probe,
532
+};
533
+
505534 static const struct of_device_id of_hisi_thermal_match[] = {
506535 {
507536 .compatible = "hisilicon,tsensor",
508
- .data = hi6220_thermal_probe
537
+ .data = &hi6220_ops,
509538 },
510539 {
511540 .compatible = "hisilicon,hi3660-tsensor",
512
- .data = hi3660_thermal_probe
541
+ .data = &hi3660_ops,
513542 },
514543 { /* end */ }
515544 };
....@@ -520,16 +549,18 @@
520549 {
521550 struct thermal_zone_device *tzd = sensor->tzd;
522551
523
- tzd->ops->set_mode(tzd,
524
- on ? THERMAL_DEVICE_ENABLED : THERMAL_DEVICE_DISABLED);
552
+ if (on)
553
+ thermal_zone_device_enable(tzd);
554
+ else
555
+ thermal_zone_device_disable(tzd);
525556 }
526557
527558 static int hisi_thermal_probe(struct platform_device *pdev)
528559 {
529560 struct hisi_thermal_data *data;
530
- int (*platform_probe)(struct hisi_thermal_data *);
531561 struct device *dev = &pdev->dev;
532
- int ret;
562
+ struct resource *res;
563
+ int i, ret;
533564
534565 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
535566 if (!data)
....@@ -537,41 +568,50 @@
537568
538569 data->pdev = pdev;
539570 platform_set_drvdata(pdev, data);
571
+ data->ops = of_device_get_match_data(dev);
540572
541
- platform_probe = of_device_get_match_data(dev);
542
- if (!platform_probe) {
543
- dev_err(dev, "failed to get probe func\n");
544
- return -EINVAL;
573
+ res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
574
+ data->regs = devm_ioremap_resource(dev, res);
575
+ if (IS_ERR(data->regs)) {
576
+ dev_err(dev, "failed to get io address\n");
577
+ return PTR_ERR(data->regs);
545578 }
546579
547
- ret = platform_probe(data);
580
+ ret = data->ops->probe(data);
548581 if (ret)
549582 return ret;
550583
551
- ret = hisi_thermal_register_sensor(pdev, data,
552
- &data->sensor);
553
- if (ret) {
554
- dev_err(dev, "failed to register thermal sensor: %d\n", ret);
555
- return ret;
556
- }
584
+ for (i = 0; i < data->nr_sensors; i++) {
585
+ struct hisi_thermal_sensor *sensor = &data->sensor[i];
557586
558
- ret = data->enable_sensor(data);
559
- if (ret) {
560
- dev_err(dev, "Failed to setup the sensor: %d\n", ret);
561
- return ret;
562
- }
563
-
564
- if (data->irq) {
565
- ret = devm_request_threaded_irq(dev, data->irq, NULL,
566
- hisi_thermal_alarm_irq_thread,
567
- IRQF_ONESHOT, "hisi_thermal", data);
568
- if (ret < 0) {
569
- dev_err(dev, "failed to request alarm irq: %d\n", ret);
587
+ ret = hisi_thermal_register_sensor(pdev, sensor);
588
+ if (ret) {
589
+ dev_err(dev, "failed to register thermal sensor: %d\n",
590
+ ret);
570591 return ret;
571592 }
572
- }
573593
574
- hisi_thermal_toggle_sensor(&data->sensor, true);
594
+ ret = platform_get_irq(pdev, 0);
595
+ if (ret < 0)
596
+ return ret;
597
+
598
+ ret = devm_request_threaded_irq(dev, ret, NULL,
599
+ hisi_thermal_alarm_irq_thread,
600
+ IRQF_ONESHOT, sensor->irq_name,
601
+ sensor);
602
+ if (ret < 0) {
603
+ dev_err(dev, "Failed to request alarm irq: %d\n", ret);
604
+ return ret;
605
+ }
606
+
607
+ ret = data->ops->enable_sensor(sensor);
608
+ if (ret) {
609
+ dev_err(dev, "Failed to setup the sensor: %d\n", ret);
610
+ return ret;
611
+ }
612
+
613
+ hisi_thermal_toggle_sensor(sensor, true);
614
+ }
575615
576616 return 0;
577617 }
....@@ -579,11 +619,14 @@
579619 static int hisi_thermal_remove(struct platform_device *pdev)
580620 {
581621 struct hisi_thermal_data *data = platform_get_drvdata(pdev);
582
- struct hisi_thermal_sensor *sensor = &data->sensor;
622
+ int i;
583623
584
- hisi_thermal_toggle_sensor(sensor, false);
624
+ for (i = 0; i < data->nr_sensors; i++) {
625
+ struct hisi_thermal_sensor *sensor = &data->sensor[i];
585626
586
- data->disable_sensor(data);
627
+ hisi_thermal_toggle_sensor(sensor, false);
628
+ data->ops->disable_sensor(sensor);
629
+ }
587630
588631 return 0;
589632 }
....@@ -592,8 +635,10 @@
592635 static int hisi_thermal_suspend(struct device *dev)
593636 {
594637 struct hisi_thermal_data *data = dev_get_drvdata(dev);
638
+ int i;
595639
596
- data->disable_sensor(data);
640
+ for (i = 0; i < data->nr_sensors; i++)
641
+ data->ops->disable_sensor(&data->sensor[i]);
597642
598643 return 0;
599644 }
....@@ -601,8 +646,12 @@
601646 static int hisi_thermal_resume(struct device *dev)
602647 {
603648 struct hisi_thermal_data *data = dev_get_drvdata(dev);
649
+ int i, ret = 0;
604650
605
- return data->enable_sensor(data);
651
+ for (i = 0; i < data->nr_sensors; i++)
652
+ ret |= data->ops->enable_sensor(&data->sensor[i]);
653
+
654
+ return ret;
606655 }
607656 #endif
608657