^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Generic ADC thermal driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2016 NVIDIA CORPORATION. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Author: Laxman Dewangan <ldewangan@nvidia.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/iio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/thermal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) struct gadc_thermal_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct thermal_zone_device *tz_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct iio_channel *channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) s32 *lookup_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) int nlookup_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static int gadc_thermal_adc_to_temp(struct gadc_thermal_info *gti, int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) int temp, temp_hi, temp_lo, adc_hi, adc_lo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (!gti->lookup_table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) for (i = 0; i < gti->nlookup_table; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (val >= gti->lookup_table[2 * i + 1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) if (i == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) temp = gti->lookup_table[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) } else if (i >= gti->nlookup_table) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) temp = gti->lookup_table[2 * (gti->nlookup_table - 1)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) adc_hi = gti->lookup_table[2 * i - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) adc_lo = gti->lookup_table[2 * i + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) temp_hi = gti->lookup_table[2 * i - 2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) temp_lo = gti->lookup_table[2 * i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) temp = temp_hi + mult_frac(temp_lo - temp_hi, val - adc_hi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) adc_lo - adc_hi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) return temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) static int gadc_thermal_get_temp(void *data, int *temp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct gadc_thermal_info *gti = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) ret = iio_read_channel_processed(gti->channel, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) dev_err(gti->dev, "IIO channel read failed %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) *temp = gadc_thermal_adc_to_temp(gti, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static const struct thermal_zone_of_device_ops gadc_thermal_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) .get_temp = gadc_thermal_get_temp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) static int gadc_thermal_read_linear_lookup_table(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct gadc_thermal_info *gti)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct device_node *np = dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) enum iio_chan_type chan_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) int ntable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) ntable = of_property_count_elems_of_size(np, "temperature-lookup-table",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (ntable <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) ret = iio_get_channel_type(gti->channel, &chan_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (ret || chan_type != IIO_TEMP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) dev_notice(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) "no lookup table, assuming DAC channel returns milliCelcius\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (ntable % 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) dev_err(dev, "Pair of temperature vs ADC read value missing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) gti->lookup_table = devm_kcalloc(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) ntable, sizeof(*gti->lookup_table),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (!gti->lookup_table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) ret = of_property_read_u32_array(np, "temperature-lookup-table",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) (u32 *)gti->lookup_table, ntable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) dev_err(dev, "Failed to read temperature lookup table: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) gti->nlookup_table = ntable / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static int gadc_thermal_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) struct gadc_thermal_info *gti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (!pdev->dev.of_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) dev_err(&pdev->dev, "Only DT based supported\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) gti = devm_kzalloc(&pdev->dev, sizeof(*gti), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (!gti)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) gti->channel = devm_iio_channel_get(&pdev->dev, "sensor-channel");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (IS_ERR(gti->channel)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) ret = PTR_ERR(gti->channel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (ret != -EPROBE_DEFER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) dev_err(&pdev->dev, "IIO channel not found: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) ret = gadc_thermal_read_linear_lookup_table(&pdev->dev, gti);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) gti->dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) platform_set_drvdata(pdev, gti);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) gti->tz_dev = devm_thermal_zone_of_sensor_register(&pdev->dev, 0, gti,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) &gadc_thermal_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if (IS_ERR(gti->tz_dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) ret = PTR_ERR(gti->tz_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (ret != -EPROBE_DEFER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) "Thermal zone sensor register failed: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static const struct of_device_id of_adc_thermal_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) { .compatible = "generic-adc-thermal", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) MODULE_DEVICE_TABLE(of, of_adc_thermal_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static struct platform_driver gadc_thermal_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) .name = "generic-adc-thermal",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) .of_match_table = of_adc_thermal_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) .probe = gadc_thermal_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) module_platform_driver(gadc_thermal_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) MODULE_DESCRIPTION("Generic ADC thermal driver using IIO framework with DT");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) MODULE_LICENSE("GPL v2");