hc
2024-01-03 2f7c68cb55ecb7331f2381deb497c27155f32faf
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,30 @@
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;
427
-
428
- data->sensor.id = HI3660_DEFAULT_SENSOR;
434
+ data->sensor[0].id = HI3660_BIG_SENSOR;
435
+ data->sensor[0].irq_name = "tsensor_a73";
436
+ data->sensor[0].data = data;
429437
430438 return 0;
431439 }
432440
433441 static int hisi_thermal_get_temp(void *__data, int *temp)
434442 {
435
- struct hisi_thermal_data *data = __data;
436
- struct hisi_thermal_sensor *sensor = &data->sensor;
443
+ struct hisi_thermal_sensor *sensor = __data;
444
+ struct hisi_thermal_data *data = sensor->data;
437445
438
- *temp = data->get_temp(data);
446
+ *temp = data->ops->get_temp(sensor);
439447
440
- dev_dbg(&data->pdev->dev, "id=%d, temp=%d, thres=%d\n",
441
- sensor->id, *temp, sensor->thres_temp);
448
+ dev_dbg(&data->pdev->dev, "tzd=%p, id=%d, temp=%d, thres=%d\n",
449
+ sensor->tzd, sensor->id, *temp, sensor->thres_temp);
442450
443451 return 0;
444452 }
....@@ -449,38 +457,39 @@
449457
450458 static irqreturn_t hisi_thermal_alarm_irq_thread(int irq, void *dev)
451459 {
452
- struct hisi_thermal_data *data = dev;
453
- struct hisi_thermal_sensor *sensor = &data->sensor;
460
+ struct hisi_thermal_sensor *sensor = dev;
461
+ struct hisi_thermal_data *data = sensor->data;
454462 int temp = 0;
455463
456
- data->irq_handler(data);
464
+ data->ops->irq_handler(sensor);
457465
458
- hisi_thermal_get_temp(data, &temp);
466
+ hisi_thermal_get_temp(sensor, &temp);
459467
460468 if (temp >= sensor->thres_temp) {
461
- dev_crit(&data->pdev->dev, "THERMAL ALARM: %d > %d\n",
462
- temp, sensor->thres_temp);
469
+ dev_crit(&data->pdev->dev,
470
+ "sensor <%d> THERMAL ALARM: %d > %d\n",
471
+ sensor->id, temp, sensor->thres_temp);
463472
464
- thermal_zone_device_update(data->sensor.tzd,
473
+ thermal_zone_device_update(sensor->tzd,
465474 THERMAL_EVENT_UNSPECIFIED);
466475
467476 } else {
468
- dev_crit(&data->pdev->dev, "THERMAL ALARM stopped: %d < %d\n",
469
- temp, sensor->thres_temp);
477
+ dev_crit(&data->pdev->dev,
478
+ "sensor <%d> THERMAL ALARM stopped: %d < %d\n",
479
+ sensor->id, temp, sensor->thres_temp);
470480 }
471481
472482 return IRQ_HANDLED;
473483 }
474484
475485 static int hisi_thermal_register_sensor(struct platform_device *pdev,
476
- struct hisi_thermal_data *data,
477486 struct hisi_thermal_sensor *sensor)
478487 {
479488 int ret, i;
480489 const struct thermal_trip *trip;
481490
482491 sensor->tzd = devm_thermal_zone_of_sensor_register(&pdev->dev,
483
- sensor->id, data,
492
+ sensor->id, sensor,
484493 &hisi_of_thermal_ops);
485494 if (IS_ERR(sensor->tzd)) {
486495 ret = PTR_ERR(sensor->tzd);
....@@ -502,14 +511,30 @@
502511 return 0;
503512 }
504513
514
+static const struct hisi_thermal_ops hi6220_ops = {
515
+ .get_temp = hi6220_thermal_get_temp,
516
+ .enable_sensor = hi6220_thermal_enable_sensor,
517
+ .disable_sensor = hi6220_thermal_disable_sensor,
518
+ .irq_handler = hi6220_thermal_irq_handler,
519
+ .probe = hi6220_thermal_probe,
520
+};
521
+
522
+static const struct hisi_thermal_ops hi3660_ops = {
523
+ .get_temp = hi3660_thermal_get_temp,
524
+ .enable_sensor = hi3660_thermal_enable_sensor,
525
+ .disable_sensor = hi3660_thermal_disable_sensor,
526
+ .irq_handler = hi3660_thermal_irq_handler,
527
+ .probe = hi3660_thermal_probe,
528
+};
529
+
505530 static const struct of_device_id of_hisi_thermal_match[] = {
506531 {
507532 .compatible = "hisilicon,tsensor",
508
- .data = hi6220_thermal_probe
533
+ .data = &hi6220_ops,
509534 },
510535 {
511536 .compatible = "hisilicon,hi3660-tsensor",
512
- .data = hi3660_thermal_probe
537
+ .data = &hi3660_ops,
513538 },
514539 { /* end */ }
515540 };
....@@ -520,16 +545,18 @@
520545 {
521546 struct thermal_zone_device *tzd = sensor->tzd;
522547
523
- tzd->ops->set_mode(tzd,
524
- on ? THERMAL_DEVICE_ENABLED : THERMAL_DEVICE_DISABLED);
548
+ if (on)
549
+ thermal_zone_device_enable(tzd);
550
+ else
551
+ thermal_zone_device_disable(tzd);
525552 }
526553
527554 static int hisi_thermal_probe(struct platform_device *pdev)
528555 {
529556 struct hisi_thermal_data *data;
530
- int (*platform_probe)(struct hisi_thermal_data *);
531557 struct device *dev = &pdev->dev;
532
- int ret;
558
+ struct resource *res;
559
+ int i, ret;
533560
534561 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
535562 if (!data)
....@@ -537,41 +564,50 @@
537564
538565 data->pdev = pdev;
539566 platform_set_drvdata(pdev, data);
567
+ data->ops = of_device_get_match_data(dev);
540568
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;
569
+ res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
570
+ data->regs = devm_ioremap_resource(dev, res);
571
+ if (IS_ERR(data->regs)) {
572
+ dev_err(dev, "failed to get io address\n");
573
+ return PTR_ERR(data->regs);
545574 }
546575
547
- ret = platform_probe(data);
576
+ ret = data->ops->probe(data);
548577 if (ret)
549578 return ret;
550579
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
- }
580
+ for (i = 0; i < data->nr_sensors; i++) {
581
+ struct hisi_thermal_sensor *sensor = &data->sensor[i];
557582
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);
583
+ ret = hisi_thermal_register_sensor(pdev, sensor);
584
+ if (ret) {
585
+ dev_err(dev, "failed to register thermal sensor: %d\n",
586
+ ret);
570587 return ret;
571588 }
572
- }
573589
574
- hisi_thermal_toggle_sensor(&data->sensor, true);
590
+ ret = platform_get_irq(pdev, 0);
591
+ if (ret < 0)
592
+ return ret;
593
+
594
+ ret = devm_request_threaded_irq(dev, ret, NULL,
595
+ hisi_thermal_alarm_irq_thread,
596
+ IRQF_ONESHOT, sensor->irq_name,
597
+ sensor);
598
+ if (ret < 0) {
599
+ dev_err(dev, "Failed to request alarm irq: %d\n", ret);
600
+ return ret;
601
+ }
602
+
603
+ ret = data->ops->enable_sensor(sensor);
604
+ if (ret) {
605
+ dev_err(dev, "Failed to setup the sensor: %d\n", ret);
606
+ return ret;
607
+ }
608
+
609
+ hisi_thermal_toggle_sensor(sensor, true);
610
+ }
575611
576612 return 0;
577613 }
....@@ -579,11 +615,14 @@
579615 static int hisi_thermal_remove(struct platform_device *pdev)
580616 {
581617 struct hisi_thermal_data *data = platform_get_drvdata(pdev);
582
- struct hisi_thermal_sensor *sensor = &data->sensor;
618
+ int i;
583619
584
- hisi_thermal_toggle_sensor(sensor, false);
620
+ for (i = 0; i < data->nr_sensors; i++) {
621
+ struct hisi_thermal_sensor *sensor = &data->sensor[i];
585622
586
- data->disable_sensor(data);
623
+ hisi_thermal_toggle_sensor(sensor, false);
624
+ data->ops->disable_sensor(sensor);
625
+ }
587626
588627 return 0;
589628 }
....@@ -592,8 +631,10 @@
592631 static int hisi_thermal_suspend(struct device *dev)
593632 {
594633 struct hisi_thermal_data *data = dev_get_drvdata(dev);
634
+ int i;
595635
596
- data->disable_sensor(data);
636
+ for (i = 0; i < data->nr_sensors; i++)
637
+ data->ops->disable_sensor(&data->sensor[i]);
597638
598639 return 0;
599640 }
....@@ -601,8 +642,12 @@
601642 static int hisi_thermal_resume(struct device *dev)
602643 {
603644 struct hisi_thermal_data *data = dev_get_drvdata(dev);
645
+ int i, ret = 0;
604646
605
- return data->enable_sensor(data);
647
+ for (i = 0; i < data->nr_sensors; i++)
648
+ ret |= data->ops->enable_sensor(&data->sensor[i]);
649
+
650
+ return ret;
606651 }
607652 #endif
608653