18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * lm77.c - Part of lm_sensors, Linux kernel modules for hardware
48c2ecf20Sopenharmony_ci *	    monitoring
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (c) 2004  Andras BALI <drewie@freemail.hu>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Heavily based on lm75.c by Frodo Looijaard <frodol@dds.nl>.  The LM77
98c2ecf20Sopenharmony_ci * is a temperature sensor and thermal window comparator with 0.5 deg
108c2ecf20Sopenharmony_ci * resolution made by National Semiconductor.  Complete datasheet can be
118c2ecf20Sopenharmony_ci * obtained at their site:
128c2ecf20Sopenharmony_ci *	http://www.national.com/pf/LM/LM77.html
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <linux/module.h>
168c2ecf20Sopenharmony_ci#include <linux/init.h>
178c2ecf20Sopenharmony_ci#include <linux/slab.h>
188c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
198c2ecf20Sopenharmony_ci#include <linux/i2c.h>
208c2ecf20Sopenharmony_ci#include <linux/hwmon.h>
218c2ecf20Sopenharmony_ci#include <linux/hwmon-sysfs.h>
228c2ecf20Sopenharmony_ci#include <linux/err.h>
238c2ecf20Sopenharmony_ci#include <linux/mutex.h>
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/* Addresses to scan */
268c2ecf20Sopenharmony_cistatic const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b,
278c2ecf20Sopenharmony_ci						I2C_CLIENT_END };
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/* The LM77 registers */
308c2ecf20Sopenharmony_ci#define LM77_REG_TEMP		0x00
318c2ecf20Sopenharmony_ci#define LM77_REG_CONF		0x01
328c2ecf20Sopenharmony_ci#define LM77_REG_TEMP_HYST	0x02
338c2ecf20Sopenharmony_ci#define LM77_REG_TEMP_CRIT	0x03
348c2ecf20Sopenharmony_ci#define LM77_REG_TEMP_MIN	0x04
358c2ecf20Sopenharmony_ci#define LM77_REG_TEMP_MAX	0x05
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cienum temp_index {
388c2ecf20Sopenharmony_ci	t_input = 0,
398c2ecf20Sopenharmony_ci	t_crit,
408c2ecf20Sopenharmony_ci	t_min,
418c2ecf20Sopenharmony_ci	t_max,
428c2ecf20Sopenharmony_ci	t_hyst,
438c2ecf20Sopenharmony_ci	t_num_temp
448c2ecf20Sopenharmony_ci};
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic const u8 temp_regs[t_num_temp] = {
478c2ecf20Sopenharmony_ci	[t_input] = LM77_REG_TEMP,
488c2ecf20Sopenharmony_ci	[t_min] = LM77_REG_TEMP_MIN,
498c2ecf20Sopenharmony_ci	[t_max] = LM77_REG_TEMP_MAX,
508c2ecf20Sopenharmony_ci	[t_crit] = LM77_REG_TEMP_CRIT,
518c2ecf20Sopenharmony_ci	[t_hyst] = LM77_REG_TEMP_HYST,
528c2ecf20Sopenharmony_ci};
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci/* Each client has this additional data */
558c2ecf20Sopenharmony_cistruct lm77_data {
568c2ecf20Sopenharmony_ci	struct i2c_client	*client;
578c2ecf20Sopenharmony_ci	struct mutex		update_lock;
588c2ecf20Sopenharmony_ci	char			valid;
598c2ecf20Sopenharmony_ci	unsigned long		last_updated;	/* In jiffies */
608c2ecf20Sopenharmony_ci	int			temp[t_num_temp]; /* index using temp_index */
618c2ecf20Sopenharmony_ci	u8			alarms;
628c2ecf20Sopenharmony_ci};
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci/* straight from the datasheet */
658c2ecf20Sopenharmony_ci#define LM77_TEMP_MIN (-55000)
668c2ecf20Sopenharmony_ci#define LM77_TEMP_MAX 125000
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci/*
698c2ecf20Sopenharmony_ci * In the temperature registers, the low 3 bits are not part of the
708c2ecf20Sopenharmony_ci * temperature values; they are the status bits.
718c2ecf20Sopenharmony_ci */
728c2ecf20Sopenharmony_cistatic inline s16 LM77_TEMP_TO_REG(int temp)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	return (temp / 500) * 8;
758c2ecf20Sopenharmony_ci}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cistatic inline int LM77_TEMP_FROM_REG(s16 reg)
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci	return (reg / 8) * 500;
808c2ecf20Sopenharmony_ci}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci/*
838c2ecf20Sopenharmony_ci * All registers are word-sized, except for the configuration register.
848c2ecf20Sopenharmony_ci * The LM77 uses the high-byte first convention.
858c2ecf20Sopenharmony_ci */
868c2ecf20Sopenharmony_cistatic u16 lm77_read_value(struct i2c_client *client, u8 reg)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	if (reg == LM77_REG_CONF)
898c2ecf20Sopenharmony_ci		return i2c_smbus_read_byte_data(client, reg);
908c2ecf20Sopenharmony_ci	else
918c2ecf20Sopenharmony_ci		return i2c_smbus_read_word_swapped(client, reg);
928c2ecf20Sopenharmony_ci}
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_cistatic int lm77_write_value(struct i2c_client *client, u8 reg, u16 value)
958c2ecf20Sopenharmony_ci{
968c2ecf20Sopenharmony_ci	if (reg == LM77_REG_CONF)
978c2ecf20Sopenharmony_ci		return i2c_smbus_write_byte_data(client, reg, value);
988c2ecf20Sopenharmony_ci	else
998c2ecf20Sopenharmony_ci		return i2c_smbus_write_word_swapped(client, reg, value);
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic struct lm77_data *lm77_update_device(struct device *dev)
1038c2ecf20Sopenharmony_ci{
1048c2ecf20Sopenharmony_ci	struct lm77_data *data = dev_get_drvdata(dev);
1058c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
1068c2ecf20Sopenharmony_ci	int i;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1118c2ecf20Sopenharmony_ci	    || !data->valid) {
1128c2ecf20Sopenharmony_ci		dev_dbg(&client->dev, "Starting lm77 update\n");
1138c2ecf20Sopenharmony_ci		for (i = 0; i < t_num_temp; i++) {
1148c2ecf20Sopenharmony_ci			data->temp[i] =
1158c2ecf20Sopenharmony_ci			  LM77_TEMP_FROM_REG(lm77_read_value(client,
1168c2ecf20Sopenharmony_ci							     temp_regs[i]));
1178c2ecf20Sopenharmony_ci		}
1188c2ecf20Sopenharmony_ci		data->alarms =
1198c2ecf20Sopenharmony_ci			lm77_read_value(client, LM77_REG_TEMP) & 0x0007;
1208c2ecf20Sopenharmony_ci		data->last_updated = jiffies;
1218c2ecf20Sopenharmony_ci		data->valid = 1;
1228c2ecf20Sopenharmony_ci	}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	return data;
1278c2ecf20Sopenharmony_ci}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci/* sysfs stuff */
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_cistatic ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
1328c2ecf20Sopenharmony_ci			 char *buf)
1338c2ecf20Sopenharmony_ci{
1348c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1358c2ecf20Sopenharmony_ci	struct lm77_data *data = lm77_update_device(dev);
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", data->temp[attr->index]);
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_cistatic ssize_t temp_hyst_show(struct device *dev,
1418c2ecf20Sopenharmony_ci			      struct device_attribute *devattr, char *buf)
1428c2ecf20Sopenharmony_ci{
1438c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1448c2ecf20Sopenharmony_ci	struct lm77_data *data = lm77_update_device(dev);
1458c2ecf20Sopenharmony_ci	int nr = attr->index;
1468c2ecf20Sopenharmony_ci	int temp;
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	temp = nr == t_min ? data->temp[nr] + data->temp[t_hyst] :
1498c2ecf20Sopenharmony_ci			     data->temp[nr] - data->temp[t_hyst];
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", temp);
1528c2ecf20Sopenharmony_ci}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cistatic ssize_t temp_store(struct device *dev,
1558c2ecf20Sopenharmony_ci			  struct device_attribute *devattr, const char *buf,
1568c2ecf20Sopenharmony_ci			  size_t count)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1598c2ecf20Sopenharmony_ci	struct lm77_data *data = dev_get_drvdata(dev);
1608c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
1618c2ecf20Sopenharmony_ci	int nr = attr->index;
1628c2ecf20Sopenharmony_ci	long val;
1638c2ecf20Sopenharmony_ci	int err;
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	err = kstrtol(buf, 10, &val);
1668c2ecf20Sopenharmony_ci	if (err)
1678c2ecf20Sopenharmony_ci		return err;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	val = clamp_val(val, LM77_TEMP_MIN, LM77_TEMP_MAX);
1708c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
1718c2ecf20Sopenharmony_ci	data->temp[nr] = val;
1728c2ecf20Sopenharmony_ci	lm77_write_value(client, temp_regs[nr], LM77_TEMP_TO_REG(val));
1738c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
1748c2ecf20Sopenharmony_ci	return count;
1758c2ecf20Sopenharmony_ci}
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci/*
1788c2ecf20Sopenharmony_ci * hysteresis is stored as a relative value on the chip, so it has to be
1798c2ecf20Sopenharmony_ci * converted first.
1808c2ecf20Sopenharmony_ci */
1818c2ecf20Sopenharmony_cistatic ssize_t temp_hyst_store(struct device *dev,
1828c2ecf20Sopenharmony_ci			       struct device_attribute *devattr,
1838c2ecf20Sopenharmony_ci			       const char *buf, size_t count)
1848c2ecf20Sopenharmony_ci{
1858c2ecf20Sopenharmony_ci	struct lm77_data *data = dev_get_drvdata(dev);
1868c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
1878c2ecf20Sopenharmony_ci	long val;
1888c2ecf20Sopenharmony_ci	int err;
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	err = kstrtol(buf, 10, &val);
1918c2ecf20Sopenharmony_ci	if (err)
1928c2ecf20Sopenharmony_ci		return err;
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
1958c2ecf20Sopenharmony_ci	val = clamp_val(data->temp[t_crit] - val, LM77_TEMP_MIN, LM77_TEMP_MAX);
1968c2ecf20Sopenharmony_ci	data->temp[t_hyst] = val;
1978c2ecf20Sopenharmony_ci	lm77_write_value(client, LM77_REG_TEMP_HYST,
1988c2ecf20Sopenharmony_ci			 LM77_TEMP_TO_REG(data->temp[t_hyst]));
1998c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
2008c2ecf20Sopenharmony_ci	return count;
2018c2ecf20Sopenharmony_ci}
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_cistatic ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
2048c2ecf20Sopenharmony_ci			  char *buf)
2058c2ecf20Sopenharmony_ci{
2068c2ecf20Sopenharmony_ci	int bitnr = to_sensor_dev_attr(attr)->index;
2078c2ecf20Sopenharmony_ci	struct lm77_data *data = lm77_update_device(dev);
2088c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
2098c2ecf20Sopenharmony_ci}
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_input, temp, t_input);
2128c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_crit, temp, t_crit);
2138c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_min, temp, t_min);
2148c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_max, temp, t_max);
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_crit_hyst, temp_hyst, t_crit);
2178c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_min_hyst, temp_hyst, t_min);
2188c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_max_hyst, temp_hyst, t_max);
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, alarm, 2);
2218c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, alarm, 0);
2228c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 1);
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_cistatic struct attribute *lm77_attrs[] = {
2258c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_input.dev_attr.attr,
2268c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_crit.dev_attr.attr,
2278c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_min.dev_attr.attr,
2288c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_max.dev_attr.attr,
2298c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
2308c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_min_hyst.dev_attr.attr,
2318c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
2328c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
2338c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
2348c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
2358c2ecf20Sopenharmony_ci	NULL
2368c2ecf20Sopenharmony_ci};
2378c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(lm77);
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci/* Return 0 if detection is successful, -ENODEV otherwise */
2408c2ecf20Sopenharmony_cistatic int lm77_detect(struct i2c_client *client, struct i2c_board_info *info)
2418c2ecf20Sopenharmony_ci{
2428c2ecf20Sopenharmony_ci	struct i2c_adapter *adapter = client->adapter;
2438c2ecf20Sopenharmony_ci	int i, cur, conf, hyst, crit, min, max;
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
2468c2ecf20Sopenharmony_ci				     I2C_FUNC_SMBUS_WORD_DATA))
2478c2ecf20Sopenharmony_ci		return -ENODEV;
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	/*
2508c2ecf20Sopenharmony_ci	 * Here comes the remaining detection.  Since the LM77 has no
2518c2ecf20Sopenharmony_ci	 * register dedicated to identification, we have to rely on the
2528c2ecf20Sopenharmony_ci	 * following tricks:
2538c2ecf20Sopenharmony_ci	 *
2548c2ecf20Sopenharmony_ci	 * 1. the high 4 bits represent the sign and thus they should
2558c2ecf20Sopenharmony_ci	 *    always be the same
2568c2ecf20Sopenharmony_ci	 * 2. the high 3 bits are unused in the configuration register
2578c2ecf20Sopenharmony_ci	 * 3. addresses 0x06 and 0x07 return the last read value
2588c2ecf20Sopenharmony_ci	 * 4. registers cycling over 8-address boundaries
2598c2ecf20Sopenharmony_ci	 *
2608c2ecf20Sopenharmony_ci	 * Word-sized registers are high-byte first.
2618c2ecf20Sopenharmony_ci	 */
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	/* addresses cycling */
2648c2ecf20Sopenharmony_ci	cur = i2c_smbus_read_word_data(client, 0);
2658c2ecf20Sopenharmony_ci	conf = i2c_smbus_read_byte_data(client, 1);
2668c2ecf20Sopenharmony_ci	hyst = i2c_smbus_read_word_data(client, 2);
2678c2ecf20Sopenharmony_ci	crit = i2c_smbus_read_word_data(client, 3);
2688c2ecf20Sopenharmony_ci	min = i2c_smbus_read_word_data(client, 4);
2698c2ecf20Sopenharmony_ci	max = i2c_smbus_read_word_data(client, 5);
2708c2ecf20Sopenharmony_ci	for (i = 8; i <= 0xff; i += 8) {
2718c2ecf20Sopenharmony_ci		if (i2c_smbus_read_byte_data(client, i + 1) != conf
2728c2ecf20Sopenharmony_ci		 || i2c_smbus_read_word_data(client, i + 2) != hyst
2738c2ecf20Sopenharmony_ci		 || i2c_smbus_read_word_data(client, i + 3) != crit
2748c2ecf20Sopenharmony_ci		 || i2c_smbus_read_word_data(client, i + 4) != min
2758c2ecf20Sopenharmony_ci		 || i2c_smbus_read_word_data(client, i + 5) != max)
2768c2ecf20Sopenharmony_ci			return -ENODEV;
2778c2ecf20Sopenharmony_ci	}
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	/* sign bits */
2808c2ecf20Sopenharmony_ci	if (((cur & 0x00f0) != 0xf0 && (cur & 0x00f0) != 0x0)
2818c2ecf20Sopenharmony_ci	 || ((hyst & 0x00f0) != 0xf0 && (hyst & 0x00f0) != 0x0)
2828c2ecf20Sopenharmony_ci	 || ((crit & 0x00f0) != 0xf0 && (crit & 0x00f0) != 0x0)
2838c2ecf20Sopenharmony_ci	 || ((min & 0x00f0) != 0xf0 && (min & 0x00f0) != 0x0)
2848c2ecf20Sopenharmony_ci	 || ((max & 0x00f0) != 0xf0 && (max & 0x00f0) != 0x0))
2858c2ecf20Sopenharmony_ci		return -ENODEV;
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	/* unused bits */
2888c2ecf20Sopenharmony_ci	if (conf & 0xe0)
2898c2ecf20Sopenharmony_ci		return -ENODEV;
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	/* 0x06 and 0x07 return the last read value */
2928c2ecf20Sopenharmony_ci	cur = i2c_smbus_read_word_data(client, 0);
2938c2ecf20Sopenharmony_ci	if (i2c_smbus_read_word_data(client, 6) != cur
2948c2ecf20Sopenharmony_ci	 || i2c_smbus_read_word_data(client, 7) != cur)
2958c2ecf20Sopenharmony_ci		return -ENODEV;
2968c2ecf20Sopenharmony_ci	hyst = i2c_smbus_read_word_data(client, 2);
2978c2ecf20Sopenharmony_ci	if (i2c_smbus_read_word_data(client, 6) != hyst
2988c2ecf20Sopenharmony_ci	 || i2c_smbus_read_word_data(client, 7) != hyst)
2998c2ecf20Sopenharmony_ci		return -ENODEV;
3008c2ecf20Sopenharmony_ci	min = i2c_smbus_read_word_data(client, 4);
3018c2ecf20Sopenharmony_ci	if (i2c_smbus_read_word_data(client, 6) != min
3028c2ecf20Sopenharmony_ci	 || i2c_smbus_read_word_data(client, 7) != min)
3038c2ecf20Sopenharmony_ci		return -ENODEV;
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	strlcpy(info->type, "lm77", I2C_NAME_SIZE);
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	return 0;
3088c2ecf20Sopenharmony_ci}
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_cistatic void lm77_init_client(struct i2c_client *client)
3118c2ecf20Sopenharmony_ci{
3128c2ecf20Sopenharmony_ci	/* Initialize the LM77 chip - turn off shutdown mode */
3138c2ecf20Sopenharmony_ci	int conf = lm77_read_value(client, LM77_REG_CONF);
3148c2ecf20Sopenharmony_ci	if (conf & 1)
3158c2ecf20Sopenharmony_ci		lm77_write_value(client, LM77_REG_CONF, conf & 0xfe);
3168c2ecf20Sopenharmony_ci}
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_cistatic int lm77_probe(struct i2c_client *client)
3198c2ecf20Sopenharmony_ci{
3208c2ecf20Sopenharmony_ci	struct device *dev = &client->dev;
3218c2ecf20Sopenharmony_ci	struct device *hwmon_dev;
3228c2ecf20Sopenharmony_ci	struct lm77_data *data;
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	data = devm_kzalloc(dev, sizeof(struct lm77_data), GFP_KERNEL);
3258c2ecf20Sopenharmony_ci	if (!data)
3268c2ecf20Sopenharmony_ci		return -ENOMEM;
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	data->client = client;
3298c2ecf20Sopenharmony_ci	mutex_init(&data->update_lock);
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	/* Initialize the LM77 chip */
3328c2ecf20Sopenharmony_ci	lm77_init_client(client);
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
3358c2ecf20Sopenharmony_ci							   data, lm77_groups);
3368c2ecf20Sopenharmony_ci	return PTR_ERR_OR_ZERO(hwmon_dev);
3378c2ecf20Sopenharmony_ci}
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_cistatic const struct i2c_device_id lm77_id[] = {
3408c2ecf20Sopenharmony_ci	{ "lm77", 0 },
3418c2ecf20Sopenharmony_ci	{ }
3428c2ecf20Sopenharmony_ci};
3438c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, lm77_id);
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci/* This is the driver that will be inserted */
3468c2ecf20Sopenharmony_cistatic struct i2c_driver lm77_driver = {
3478c2ecf20Sopenharmony_ci	.class		= I2C_CLASS_HWMON,
3488c2ecf20Sopenharmony_ci	.driver = {
3498c2ecf20Sopenharmony_ci		.name	= "lm77",
3508c2ecf20Sopenharmony_ci	},
3518c2ecf20Sopenharmony_ci	.probe_new	= lm77_probe,
3528c2ecf20Sopenharmony_ci	.id_table	= lm77_id,
3538c2ecf20Sopenharmony_ci	.detect		= lm77_detect,
3548c2ecf20Sopenharmony_ci	.address_list	= normal_i2c,
3558c2ecf20Sopenharmony_ci};
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_cimodule_i2c_driver(lm77_driver);
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ciMODULE_AUTHOR("Andras BALI <drewie@freemail.hu>");
3608c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("LM77 driver");
3618c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
362