blob: 4cde4b6077686c0a7e3c51e672ce051df5004a71 [file] [log] [blame]
// SPDX-License-Identifier: GPL-2.0
/*
* The driver is developed based on ntc_thermistor driver.
*
* Copyright 2020 Google LLC.
*/
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/pm_runtime.h>
#include <linux/math64.h>
#include <linux/platform_device.h>
#include <linux/err.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/platform_data/ptc_thermistor.h>
#include <linux/iio/iio.h>
#include <linux/iio/machine.h>
#include <linux/iio/driver.h>
#include <linux/iio/consumer.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <linux/thermal.h>
struct ptc_compensation {
int temp_c;
unsigned int ohm;
};
/* Order matters, ptc_match references the entries by index */
static const struct platform_device_id ptc_thermistor_id[] = {
{ "tmp6331decr,v,conf1", TYPE_TMP6331DECR_VBIAS_1_8_V_100000_OHM },
{ },
};
/*
* temp_c in descending order for PTC thermistors
*/
static const struct ptc_compensation comp_tmp6331decr_vbias_1_8v_100000_ohm[] = {
{ .temp_c = 125, .ohm = 175288 },
{ .temp_c = 120, .ohm = 170610 },
{ .temp_c = 115, .ohm = 166029 },
{ .temp_c = 110, .ohm = 161541 },
{ .temp_c = 105, .ohm = 157146 },
{ .temp_c = 100, .ohm = 152841 },
{ .temp_c = 95, .ohm = 148626 },
{ .temp_c = 90, .ohm = 144498 },
{ .temp_c = 85, .ohm = 140457 },
{ .temp_c = 80, .ohm = 136502 },
{ .temp_c = 75, .ohm = 132630 },
{ .temp_c = 70, .ohm = 128840 },
{ .temp_c = 65, .ohm = 125133 },
{ .temp_c = 60, .ohm = 121506 },
{ .temp_c = 55, .ohm = 117958 },
{ .temp_c = 50, .ohm = 114489 },
{ .temp_c = 45, .ohm = 111098 },
{ .temp_c = 40, .ohm = 107783 },
{ .temp_c = 35, .ohm = 104545 },
{ .temp_c = 30, .ohm = 101383 },
{ .temp_c = 25, .ohm = 98295 },
{ .temp_c = 20, .ohm = 95281 },
{ .temp_c = 15, .ohm = 92342 },
{ .temp_c = 10, .ohm = 89475 },
{ .temp_c = 5, .ohm = 86682 },
{ .temp_c = 0, .ohm = 83961 },
{ .temp_c = -5, .ohm = 81312 },
{ .temp_c = -10, .ohm = 78736 },
{ .temp_c = -15, .ohm = 76232 },
{ .temp_c = -20, .ohm = 73800 },
{ .temp_c = -25, .ohm = 71439 },
{ .temp_c = -30, .ohm = 69151 },
{ .temp_c = -35, .ohm = 66935 },
{ .temp_c = -40, .ohm = 64791 },
};
struct ptc_data {
struct ptc_thermistor_platform_data *pdata;
const struct ptc_compensation *comp;
int n_comp;
int offset_mc;
};
#if defined(CONFIG_OF) && IS_ENABLED(CONFIG_IIO)
static int ptc_adc_iio_read(struct ptc_thermistor_platform_data *pdata)
{
struct iio_channel *channel = pdata->chan;
int raw, uv, ret;
ret = iio_read_channel_raw(channel, &raw);
if (ret < 0) {
pr_err("read channel() error: %d\n", ret);
return ret;
}
ret = iio_convert_raw_to_processed(channel, raw, &uv, 1000);
if (ret < 0) {
/* Assume 12 bit ADC with vref at pullup_uv */
uv = (pdata->pullup_uv * (s64)raw) >> 12;
}
return uv;
}
static const struct of_device_id ptc_match[] = {
{ .compatible = "ti,tmp6331decr,vbias-1.8v-100000ohm",
.data = &ptc_thermistor_id[0] },
{ },
};
MODULE_DEVICE_TABLE(of, ptc_match);
static struct ptc_thermistor_platform_data *
ptc_thermistor_parse_dt(struct device *dev)
{
struct iio_channel *chan;
enum iio_chan_type type;
struct device_node *np = dev->of_node;
struct ptc_thermistor_platform_data *pdata;
int ret;
if (!np)
return NULL;
pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
if (!pdata)
return ERR_PTR(-ENOMEM);
chan = devm_iio_channel_get(dev, NULL);
if (IS_ERR(chan))
return ERR_CAST(chan);
ret = iio_get_channel_type(chan, &type);
if (ret < 0)
return ERR_PTR(ret);
if (type != IIO_VOLTAGE)
return ERR_PTR(-EINVAL);
if (of_property_read_u32(np, "pullup-uv", &pdata->pullup_uv))
return ERR_PTR(-ENODEV);
if (of_property_read_u32(np, "pull-ohm", &pdata->pull_ohm))
return ERR_PTR(-ENODEV);
if (of_find_property(np, "connected-positive", NULL))
pdata->connect = PTC_CONNECTED_POSITIVE;
else /* status change should be possible if not always on. */
pdata->connect = PTC_CONNECTED_GROUND;
pdata->chan = chan;
pdata->read_uv = ptc_adc_iio_read;
return pdata;
}
#else
static struct ptc_thermistor_platform_data *
ptc_thermistor_parse_dt(struct device *dev)
{
return NULL;
}
#define ptc_match NULL
#endif
static inline u64 div64_u64_safe(u64 dividend, u64 divisor)
{
if (divisor == 0 && dividend == 0)
return 0;
if (divisor == 0)
return UINT_MAX;
return div64_u64(dividend, divisor);
}
static int get_ohm_of_thermistor(struct ptc_data *data, unsigned int uv)
{
struct ptc_thermistor_platform_data *pdata = data->pdata;
u32 puv = pdata->pullup_uv;
u64 n, po;
po = pdata->pull_ohm;
if (uv == 0)
return (pdata->connect == PTC_CONNECTED_POSITIVE) ?
INT_MAX : 0;
if (uv >= puv)
return (pdata->connect == PTC_CONNECTED_POSITIVE) ?
0 : INT_MAX;
if (pdata->connect == PTC_CONNECTED_POSITIVE)
n = div_u64(po * (puv - uv), uv);
else
n = div_u64(po * uv, puv - uv);
if (n > INT_MAX)
n = INT_MAX;
return n;
}
static void lookup_comp(struct ptc_data *data, unsigned int ohm,
int *i_low, int *i_high)
{
int start, end, mid;
/*
* Handle special cases: Resistance is higher than or equal to
* resistance in first table entry, or resistance is lower or equal
* to resistance in last table entry.
* In these cases, return i_low == i_high, either pointing to the
* beginning or to the end of the table depending on the condition.
*/
if (ohm >= data->comp[0].ohm) {
*i_low = 0;
*i_high = 0;
return;
}
if (ohm <= data->comp[data->n_comp - 1].ohm) {
*i_low = data->n_comp - 1;
*i_high = data->n_comp - 1;
return;
}
/* Do a binary search on compensation table */
start = 0;
end = data->n_comp;
while (start < end) {
mid = start + (end - start) / 2;
/*
* start <= mid < end
* data->comp[start].ohm > ohm >= data->comp[end].ohm
*
* We could check for "ohm == data->comp[mid].ohm" here, but
* that is a quite unlikely condition, and we would have to
* check again after updating start. Check it at the end instead
* for simplicity.
*/
if (ohm >= data->comp[mid].ohm) {
end = mid;
} else {
start = mid + 1;
/*
* ohm >= data->comp[start].ohm might be true here,
* since we set start to mid + 1. In that case, we are
* done. We could keep going, but the condition is quite
* likely to occur, so it is worth checking for it.
*/
if (ohm >= data->comp[start].ohm)
end = start;
}
/*
* start <= end
* data->comp[start].ohm >= ohm >= data->comp[end].ohm
*/
}
/*
* start == end
* ohm >= data->comp[end].ohm
*/
*i_low = end;
if (ohm == data->comp[end].ohm)
*i_high = end;
else
*i_high = end - 1;
}
static int get_temp_mc(struct ptc_data *data, unsigned int ohm)
{
int low, high;
int temp;
lookup_comp(data, ohm, &low, &high);
if (low == high) {
/* Unable to use linear approximation */
temp = data->comp[low].temp_c * 1000;
} else {
temp = data->comp[low].temp_c * 1000 +
((data->comp[high].temp_c - data->comp[low].temp_c) *
1000 * ((int)ohm - (int)data->comp[low].ohm)) /
((int)data->comp[high].ohm - (int)data->comp[low].ohm);
}
return temp;
}
static int ptc_thermistor_get_ohm(struct ptc_data *data)
{
int read_uv;
if (data->pdata->read_ohm)
return data->pdata->read_ohm();
if (data->pdata->read_uv) {
read_uv = data->pdata->read_uv(data->pdata);
if (read_uv < 0)
return read_uv;
return get_ohm_of_thermistor(data, read_uv);
}
return -EINVAL;
}
static int ptc_read_temp(void *data, int *temp)
{
int ohm;
ohm = ptc_thermistor_get_ohm(data);
if (ohm < 0)
return ohm;
*temp = get_temp_mc(data, ohm);
return 0;
}
static ssize_t ptc_show_type(struct device *dev,
struct device_attribute *attr, char *buf)
{
return sprintf(buf, "4\n");
}
static ssize_t ptc_show_temp(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct ptc_data *data = dev_get_drvdata(dev);
int ohm;
ohm = ptc_thermistor_get_ohm(data);
if (ohm < 0)
return ohm;
return sprintf(buf, "%d\n",
get_temp_mc(data, ohm) + data->offset_mc);
}
static ssize_t ptc_show_offset(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct ptc_data *data = dev_get_drvdata(dev);
return sprintf(buf, "%d\n", data->offset_mc);
}
static ssize_t ptc_store_offset(struct device *dev,
struct device_attribute *da, const char *buf, size_t count)
{
struct ptc_data *data = dev_get_drvdata(dev);
int val;
int err;
err = kstrtoint(buf, 10, &val);
if (err)
return err;
data->offset_mc = val;
return count;
}
static SENSOR_DEVICE_ATTR(temp1_type, 0444, ptc_show_type, NULL, 0);
static SENSOR_DEVICE_ATTR(temp1_input, 0444, ptc_show_temp, NULL, 0);
static SENSOR_DEVICE_ATTR(temp1_offset,
0644, ptc_show_offset, ptc_store_offset, 0);
static struct attribute *ptc_attrs[] = {
&sensor_dev_attr_temp1_type.dev_attr.attr,
&sensor_dev_attr_temp1_input.dev_attr.attr,
&sensor_dev_attr_temp1_offset.dev_attr.attr,
NULL,
};
ATTRIBUTE_GROUPS(ptc);
static const struct thermal_zone_of_device_ops ptc_of_thermal_ops = {
.get_temp = ptc_read_temp,
};
static int ptc_thermistor_probe(struct platform_device *pdev)
{
struct thermal_zone_device *tz;
struct device *dev = &pdev->dev;
const struct of_device_id *of_id =
of_match_device(of_match_ptr(ptc_match), dev);
const struct platform_device_id *pdev_id;
struct ptc_thermistor_platform_data *pdata;
struct device *hwmon_dev;
struct ptc_data *data;
pdata = ptc_thermistor_parse_dt(dev);
if (IS_ERR(pdata))
return PTR_ERR(pdata);
else if (!pdata)
pdata = dev_get_platdata(dev);
if (!pdata) {
dev_err(dev, "No platform init data supplied.\n");
return -ENODEV;
}
/* Either one of the two is required. */
if (!pdata->read_uv && !pdata->read_ohm) {
dev_err(dev,
"Both read_uv and read_ohm missing. Need either one of the two.\n");
return -EINVAL;
}
if (pdata->read_uv && pdata->read_ohm) {
dev_warn(dev,
"Only one of read_uv and read_ohm is needed; ignoring read_uv.\n");
pdata->read_uv = NULL;
}
if (pdata->read_uv && (pdata->pullup_uv == 0 ||
pdata->pull_ohm == 0 ||
(pdata->connect != PTC_CONNECTED_POSITIVE &&
pdata->connect != PTC_CONNECTED_GROUND))) {
dev_err(dev, "Required data to use read_uv not supplied.\n");
return -EINVAL;
}
data = devm_kzalloc(dev, sizeof(struct ptc_data), GFP_KERNEL);
if (!data)
return -ENOMEM;
pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
data->pdata = pdata;
data->offset_mc = 0;
switch (pdev_id->driver_data) {
case TYPE_TMP6331DECR_VBIAS_1_8_V_100000_OHM:
data->comp = comp_tmp6331decr_vbias_1_8v_100000_ohm;
data->n_comp = ARRAY_SIZE(comp_tmp6331decr_vbias_1_8v_100000_ohm);
break;
default:
dev_err(dev, "Unknown device type: %lu(%s)\n",
pdev_id->driver_data, pdev_id->name);
return -EINVAL;
}
hwmon_dev = devm_hwmon_device_register_with_groups(dev, pdev_id->name,
data, ptc_groups);
if (IS_ERR(hwmon_dev)) {
dev_err(dev, "unable to register as hwmon device.\n");
return PTR_ERR(hwmon_dev);
}
dev_info(dev, "Thermistor type: %s successfully probed.\n",
pdev_id->name);
tz = devm_thermal_zone_of_sensor_register(dev, 0, data,
&ptc_of_thermal_ops);
if (IS_ERR(tz))
dev_dbg(dev, "Failed to register to thermal fw.\n");
return 0;
}
static struct platform_driver ptc_thermistor_driver = {
.driver = {
.name = "ptc-thermistor",
.of_match_table = of_match_ptr(ptc_match),
},
.probe = ptc_thermistor_probe,
.id_table = ptc_thermistor_id,
};
module_platform_driver(ptc_thermistor_driver);
MODULE_DESCRIPTION("PTC Thermistor Driver");
MODULE_AUTHOR("Tang Lee <tanglee@google.com>");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:ptc-thermistor");