blob: afdbb63237b9e4311d18b81194be35b42ae56eea [file] [log] [blame]
Thomas Gleixner74ba9202019-05-20 09:19:02 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Shubhrajyoti Dcaaa0f32010-10-28 20:31:44 +02003 * lm75.c - Part of lm_sensors, Linux kernel modules for hardware
4 * monitoring
5 * Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
Shubhrajyoti Dcaaa0f32010-10-28 20:31:44 +02006 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/module.h>
9#include <linux/init.h>
10#include <linux/slab.h>
11#include <linux/jiffies.h>
12#include <linux/i2c.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040013#include <linux/hwmon.h>
Jean Delvare9ca8e40c82007-05-08 17:22:01 +020014#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040015#include <linux/err.h>
Javier Martinez Canillase97a45f2017-02-24 10:13:02 -030016#include <linux/of_device.h>
Eduardo Valentin22e73182013-07-16 14:54:55 -040017#include <linux/of.h>
Guenter Roecke65365f2016-06-19 17:49:19 -070018#include <linux/regmap.h>
Iker Perez del Palomar Sustatxa4b5be3c2019-08-08 09:02:46 +010019#include <linux/util_macros.h>
Alban Bedel707d1512020-10-01 16:57:38 +020020#include <linux/regulator/consumer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "lm75.h"
22
David Brownell01a52392008-04-21 12:10:53 -070023/*
24 * This driver handles the LM75 and compatible digital temperature sensors.
David Brownell01a52392008-04-21 12:10:53 -070025 */
26
David Brownell9ebd3d82008-05-03 19:33:15 -070027enum lm75_type { /* keep sorted in alphabetical order */
Michael Henneriche96f9d82011-10-13 04:43:31 -040028 adt75,
Jean Delvare1f86df42009-12-14 21:17:26 +010029 ds1775,
David Brownell9ebd3d82008-05-03 19:33:15 -070030 ds75,
Jean Delvare3fbc81e2013-05-04 14:49:36 +020031 ds7505,
Arnaud Ebalardc98d6c62013-11-09 18:39:14 +010032 g751,
Jean Delvare1f86df42009-12-14 21:17:26 +010033 lm75,
David Brownell9ebd3d82008-05-03 19:33:15 -070034 lm75a,
Michael Thalmeier799fc602014-11-18 17:08:04 +010035 lm75b,
David Brownell9ebd3d82008-05-03 19:33:15 -070036 max6625,
37 max6626,
Kun Yia54ca772018-09-11 13:25:58 -070038 max31725,
David Brownell9ebd3d82008-05-03 19:33:15 -070039 mcp980x,
Daniel Mack557c7ff2019-07-11 14:45:04 +020040 pct2075,
David Brownell9ebd3d82008-05-03 19:33:15 -070041 stds75,
Jagan Teki2e9a41b2018-12-06 02:44:22 +053042 stlm75,
David Brownell9ebd3d82008-05-03 19:33:15 -070043 tcn75,
44 tmp100,
45 tmp101,
Shubhrajyoti Datta6d034052010-05-27 19:59:03 +020046 tmp105,
Frans Klaverc83959f2014-06-26 11:21:11 +020047 tmp112,
David Brownell9ebd3d82008-05-03 19:33:15 -070048 tmp175,
49 tmp275,
50 tmp75,
Iker Perez del Palomar Sustatxa39abe9d2019-05-03 17:15:00 +010051 tmp75b,
Ben Gardner9c32e812015-10-07 21:55:20 -050052 tmp75c,
Robert Markoec081f92021-04-29 14:11:49 +020053 tmp1075,
David Brownell9ebd3d82008-05-03 19:33:15 -070054};
55
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +010056/**
57 * struct lm75_params - lm75 configuration parameters.
58 * @set_mask: Bits to set in configuration register when configuring
59 * the chip.
60 * @clr_mask: Bits to clear in configuration register when configuring
61 * the chip.
62 * @default_resolution: Default number of bits to represent the temperature
63 * value.
64 * @resolution_limits: Limit register resolution. Optional. Should be set if
65 * the resolution of limit registers does not match the
66 * resolution of the temperature register.
Iker Perez del Palomar Sustatxa7f1a3002019-08-08 09:02:45 +010067 * @resolutions: List of resolutions associated with sample times.
68 * Optional. Should be set if num_sample_times is larger
69 * than 1, and if the resolution changes with sample times.
70 * If set, number of entries must match num_sample_times.
71 * @default_sample_time:Sample time to be set by default.
72 * @num_sample_times: Number of possible sample times to be set. Optional.
73 * Should be set if the number of sample times is larger
74 * than one.
75 * @sample_times: All the possible sample times to be set. Mandatory if
76 * num_sample_times is larger than 1. If set, number of
77 * entries must match num_sample_times.
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +010078 */
79
80struct lm75_params {
Iker Perez del Palomar Sustatxa7f1a3002019-08-08 09:02:45 +010081 u8 set_mask;
82 u8 clr_mask;
83 u8 default_resolution;
84 u8 resolution_limits;
85 const u8 *resolutions;
86 unsigned int default_sample_time;
87 u8 num_sample_times;
88 const unsigned int *sample_times;
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +010089};
90
Jean Delvare8ff69ee2008-08-10 22:56:16 +020091/* Addresses scanned */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050092static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/* The LM75 registers */
Guenter Roecke65365f2016-06-19 17:49:19 -070096#define LM75_REG_TEMP 0x00
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#define LM75_REG_CONF 0x01
Guenter Roecke65365f2016-06-19 17:49:19 -070098#define LM75_REG_HYST 0x02
99#define LM75_REG_MAX 0x03
Guenter Roeckd7a85cd2019-08-08 19:28:51 -0700100#define PCT2075_REG_IDLE 0x04
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102/* Each client has this additional data */
103struct lm75_data {
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +0100104 struct i2c_client *client;
105 struct regmap *regmap;
Alban Bedel707d1512020-10-01 16:57:38 +0200106 struct regulator *vs;
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +0100107 u8 orig_conf;
108 u8 current_conf;
109 u8 resolution; /* In bits, 9 to 16 */
110 unsigned int sample_time; /* In ms */
111 enum lm75_type kind;
112 const struct lm75_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113};
114
David Brownell01a52392008-04-21 12:10:53 -0700115/*-----------------------------------------------------------------------*/
Guenter Roeck7db0db32019-08-08 12:53:03 -0700116
117static const u8 lm75_sample_set_masks[] = { 0 << 5, 1 << 5, 2 << 5, 3 << 5 };
118
119#define LM75_SAMPLE_CLEAR_MASK (3 << 5)
120
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +0100121/* The structure below stores the configuration values of the supported devices.
122 * In case of being supported multiple configurations, the default one must
123 * always be the first element of the array
124 */
125static const struct lm75_params device_params[] = {
126 [adt75] = {
127 .clr_mask = 1 << 5, /* not one-shot mode */
128 .default_resolution = 12,
Iker Perez del Palomar Sustatxa35cd1802019-09-04 12:56:21 +0100129 .default_sample_time = MSEC_PER_SEC / 10,
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +0100130 },
131 [ds1775] = {
132 .clr_mask = 3 << 5,
133 .set_mask = 2 << 5, /* 11-bit mode */
134 .default_resolution = 11,
Iker Perez del Palomar Sustatxa35cd1802019-09-04 12:56:21 +0100135 .default_sample_time = 500,
Guenter Roeck7db0db32019-08-08 12:53:03 -0700136 .num_sample_times = 4,
Iker Perez del Palomar Sustatxa35cd1802019-09-04 12:56:21 +0100137 .sample_times = (unsigned int []){ 125, 250, 500, 1000 },
Guenter Roeck7db0db32019-08-08 12:53:03 -0700138 .resolutions = (u8 []) {9, 10, 11, 12 },
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +0100139 },
140 [ds75] = {
141 .clr_mask = 3 << 5,
142 .set_mask = 2 << 5, /* 11-bit mode */
143 .default_resolution = 11,
Guenter Roeck7db0db32019-08-08 12:53:03 -0700144 .default_sample_time = 600,
145 .num_sample_times = 4,
146 .sample_times = (unsigned int []){ 150, 300, 600, 1200 },
147 .resolutions = (u8 []) {9, 10, 11, 12 },
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +0100148 },
149 [stds75] = {
150 .clr_mask = 3 << 5,
151 .set_mask = 2 << 5, /* 11-bit mode */
152 .default_resolution = 11,
Guenter Roeck7db0db32019-08-08 12:53:03 -0700153 .default_sample_time = 600,
154 .num_sample_times = 4,
155 .sample_times = (unsigned int []){ 150, 300, 600, 1200 },
156 .resolutions = (u8 []) {9, 10, 11, 12 },
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +0100157 },
158 [stlm75] = {
159 .default_resolution = 9,
Iker Perez del Palomar Sustatxa35cd1802019-09-04 12:56:21 +0100160 .default_sample_time = MSEC_PER_SEC / 6,
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +0100161 },
162 [ds7505] = {
163 .set_mask = 3 << 5, /* 12-bit mode*/
164 .default_resolution = 12,
Guenter Roeck7db0db32019-08-08 12:53:03 -0700165 .default_sample_time = 200,
166 .num_sample_times = 4,
167 .sample_times = (unsigned int []){ 25, 50, 100, 200 },
168 .resolutions = (u8 []) {9, 10, 11, 12 },
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +0100169 },
170 [g751] = {
171 .default_resolution = 9,
Iker Perez del Palomar Sustatxa35cd1802019-09-04 12:56:21 +0100172 .default_sample_time = MSEC_PER_SEC / 10,
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +0100173 },
174 [lm75] = {
175 .default_resolution = 9,
Iker Perez del Palomar Sustatxa35cd1802019-09-04 12:56:21 +0100176 .default_sample_time = MSEC_PER_SEC / 10,
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +0100177 },
178 [lm75a] = {
179 .default_resolution = 9,
Iker Perez del Palomar Sustatxa35cd1802019-09-04 12:56:21 +0100180 .default_sample_time = MSEC_PER_SEC / 10,
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +0100181 },
182 [lm75b] = {
183 .default_resolution = 11,
Iker Perez del Palomar Sustatxa35cd1802019-09-04 12:56:21 +0100184 .default_sample_time = MSEC_PER_SEC / 10,
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +0100185 },
186 [max6625] = {
187 .default_resolution = 9,
Iker Perez del Palomar Sustatxa35cd1802019-09-04 12:56:21 +0100188 .default_sample_time = MSEC_PER_SEC / 7,
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +0100189 },
190 [max6626] = {
191 .default_resolution = 12,
Iker Perez del Palomar Sustatxa35cd1802019-09-04 12:56:21 +0100192 .default_sample_time = MSEC_PER_SEC / 7,
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +0100193 .resolution_limits = 9,
194 },
195 [max31725] = {
196 .default_resolution = 16,
Iker Perez del Palomar Sustatxa35cd1802019-09-04 12:56:21 +0100197 .default_sample_time = MSEC_PER_SEC / 20,
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +0100198 },
199 [tcn75] = {
200 .default_resolution = 9,
Iker Perez del Palomar Sustatxa35cd1802019-09-04 12:56:21 +0100201 .default_sample_time = MSEC_PER_SEC / 18,
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +0100202 },
203 [pct2075] = {
204 .default_resolution = 11,
205 .default_sample_time = MSEC_PER_SEC / 10,
Guenter Roeckd7a85cd2019-08-08 19:28:51 -0700206 .num_sample_times = 31,
207 .sample_times = (unsigned int []){ 100, 200, 300, 400, 500, 600,
208 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700,
209 1800, 1900, 2000, 2100, 2200, 2300, 2400, 2500, 2600, 2700,
210 2800, 2900, 3000, 3100 },
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +0100211 },
212 [mcp980x] = {
213 .set_mask = 3 << 5, /* 12-bit mode */
214 .clr_mask = 1 << 7, /* not one-shot mode */
215 .default_resolution = 12,
216 .resolution_limits = 9,
Guenter Roeck7db0db32019-08-08 12:53:03 -0700217 .default_sample_time = 240,
218 .num_sample_times = 4,
Iker Perez del Palomar Sustatxa35cd1802019-09-04 12:56:21 +0100219 .sample_times = (unsigned int []){ 30, 60, 120, 240 },
Guenter Roeck7db0db32019-08-08 12:53:03 -0700220 .resolutions = (u8 []) {9, 10, 11, 12 },
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +0100221 },
222 [tmp100] = {
223 .set_mask = 3 << 5, /* 12-bit mode */
224 .clr_mask = 1 << 7, /* not one-shot mode */
225 .default_resolution = 12,
Guenter Roeck7db0db32019-08-08 12:53:03 -0700226 .default_sample_time = 320,
227 .num_sample_times = 4,
Iker Perez del Palomar Sustatxa35cd1802019-09-04 12:56:21 +0100228 .sample_times = (unsigned int []){ 40, 80, 160, 320 },
Guenter Roeck7db0db32019-08-08 12:53:03 -0700229 .resolutions = (u8 []) {9, 10, 11, 12 },
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +0100230 },
231 [tmp101] = {
232 .set_mask = 3 << 5, /* 12-bit mode */
233 .clr_mask = 1 << 7, /* not one-shot mode */
234 .default_resolution = 12,
Guenter Roeck7db0db32019-08-08 12:53:03 -0700235 .default_sample_time = 320,
236 .num_sample_times = 4,
Iker Perez del Palomar Sustatxa35cd1802019-09-04 12:56:21 +0100237 .sample_times = (unsigned int []){ 40, 80, 160, 320 },
Guenter Roeck7db0db32019-08-08 12:53:03 -0700238 .resolutions = (u8 []) {9, 10, 11, 12 },
239 },
240 [tmp105] = {
241 .set_mask = 3 << 5, /* 12-bit mode */
242 .clr_mask = 1 << 7, /* not one-shot mode*/
243 .default_resolution = 12,
244 .default_sample_time = 220,
245 .num_sample_times = 4,
Iker Perez del Palomar Sustatxa35cd1802019-09-04 12:56:21 +0100246 .sample_times = (unsigned int []){ 28, 55, 110, 220 },
Guenter Roeck7db0db32019-08-08 12:53:03 -0700247 .resolutions = (u8 []) {9, 10, 11, 12 },
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +0100248 },
249 [tmp112] = {
Iker Perez del Palomar Sustatxa35cd1802019-09-04 12:56:21 +0100250 .set_mask = 3 << 5, /* 8 samples / second */
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +0100251 .clr_mask = 1 << 7, /* no one-shot mode*/
252 .default_resolution = 12,
Iker Perez del Palomar Sustatxa35cd1802019-09-04 12:56:21 +0100253 .default_sample_time = 125,
Guenter Roeckcee04a02019-08-08 19:12:55 -0700254 .num_sample_times = 4,
255 .sample_times = (unsigned int []){ 125, 250, 1000, 4000 },
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +0100256 },
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +0100257 [tmp175] = {
258 .set_mask = 3 << 5, /* 12-bit mode */
259 .clr_mask = 1 << 7, /* not one-shot mode*/
260 .default_resolution = 12,
Guenter Roeck7db0db32019-08-08 12:53:03 -0700261 .default_sample_time = 220,
262 .num_sample_times = 4,
Iker Perez del Palomar Sustatxa35cd1802019-09-04 12:56:21 +0100263 .sample_times = (unsigned int []){ 28, 55, 110, 220 },
Guenter Roeck7db0db32019-08-08 12:53:03 -0700264 .resolutions = (u8 []) {9, 10, 11, 12 },
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +0100265 },
266 [tmp275] = {
267 .set_mask = 3 << 5, /* 12-bit mode */
268 .clr_mask = 1 << 7, /* not one-shot mode*/
269 .default_resolution = 12,
Guenter Roeck7db0db32019-08-08 12:53:03 -0700270 .default_sample_time = 220,
271 .num_sample_times = 4,
Iker Perez del Palomar Sustatxa35cd1802019-09-04 12:56:21 +0100272 .sample_times = (unsigned int []){ 28, 55, 110, 220 },
Guenter Roeck7db0db32019-08-08 12:53:03 -0700273 .resolutions = (u8 []) {9, 10, 11, 12 },
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +0100274 },
275 [tmp75] = {
276 .set_mask = 3 << 5, /* 12-bit mode */
277 .clr_mask = 1 << 7, /* not one-shot mode*/
278 .default_resolution = 12,
Guenter Roeck7db0db32019-08-08 12:53:03 -0700279 .default_sample_time = 220,
280 .num_sample_times = 4,
Iker Perez del Palomar Sustatxa35cd1802019-09-04 12:56:21 +0100281 .sample_times = (unsigned int []){ 28, 55, 110, 220 },
Guenter Roeck7db0db32019-08-08 12:53:03 -0700282 .resolutions = (u8 []) {9, 10, 11, 12 },
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +0100283 },
284 [tmp75b] = { /* not one-shot mode, Conversion rate 37Hz */
285 .clr_mask = 1 << 7 | 3 << 5,
286 .default_resolution = 12,
287 .default_sample_time = MSEC_PER_SEC / 37,
Iker Perez del Palomar Sustatxa7f1a3002019-08-08 09:02:45 +0100288 .sample_times = (unsigned int []){ MSEC_PER_SEC / 37,
289 MSEC_PER_SEC / 18,
290 MSEC_PER_SEC / 9, MSEC_PER_SEC / 4 },
291 .num_sample_times = 4,
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +0100292 },
293 [tmp75c] = {
294 .clr_mask = 1 << 5, /*not one-shot mode*/
295 .default_resolution = 12,
Iker Perez del Palomar Sustatxa35cd1802019-09-04 12:56:21 +0100296 .default_sample_time = MSEC_PER_SEC / 12,
Robert Markoec081f92021-04-29 14:11:49 +0200297 },
298 [tmp1075] = { /* not one-shot mode, 27.5 ms sample rate */
299 .clr_mask = 1 << 5 | 1 << 6 | 1 << 7,
300 .default_resolution = 12,
301 .default_sample_time = 28,
302 .num_sample_times = 4,
303 .sample_times = (unsigned int []){ 28, 55, 110, 220 },
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +0100304 }
305};
David Brownell01a52392008-04-21 12:10:53 -0700306
Eduardo Valentin22e73182013-07-16 14:54:55 -0400307static inline long lm75_reg_to_mc(s16 temp, u8 resolution)
308{
309 return ((temp >> (16 - resolution)) * 1000) >> (resolution - 8);
310}
311
Iker Perez del Palomar Sustatxa58608cf2019-08-08 09:02:44 +0100312static int lm75_write_config(struct lm75_data *data, u8 set_mask,
313 u8 clr_mask)
314{
315 u8 value;
316
317 clr_mask |= LM75_SHUTDOWN;
318 value = data->current_conf & ~clr_mask;
319 value |= set_mask;
320
321 if (data->current_conf != value) {
322 s32 err;
323
324 err = i2c_smbus_write_byte_data(data->client, LM75_REG_CONF,
325 value);
326 if (err)
327 return err;
328 data->current_conf = value;
329 }
330 return 0;
331}
332
Guenter Roeck08b02432016-06-19 19:15:05 -0700333static int lm75_read(struct device *dev, enum hwmon_sensor_types type,
334 u32 attr, int channel, long *val)
Eduardo Valentin22e73182013-07-16 14:54:55 -0400335{
Guenter Roecke65365f2016-06-19 17:49:19 -0700336 struct lm75_data *data = dev_get_drvdata(dev);
Guenter Roeck08b02432016-06-19 19:15:05 -0700337 unsigned int regval;
338 int err, reg;
Eduardo Valentin22e73182013-07-16 14:54:55 -0400339
Guenter Roeck08b02432016-06-19 19:15:05 -0700340 switch (type) {
341 case hwmon_chip:
342 switch (attr) {
343 case hwmon_chip_update_interval:
344 *val = data->sample_time;
Luis de Bethencourtccffe772018-01-17 18:24:48 +0000345 break;
Guenter Roeck08b02432016-06-19 19:15:05 -0700346 default:
347 return -EINVAL;
348 }
349 break;
350 case hwmon_temp:
351 switch (attr) {
352 case hwmon_temp_input:
353 reg = LM75_REG_TEMP;
354 break;
355 case hwmon_temp_max:
356 reg = LM75_REG_MAX;
357 break;
358 case hwmon_temp_max_hyst:
359 reg = LM75_REG_HYST;
360 break;
361 default:
362 return -EINVAL;
363 }
364 err = regmap_read(data->regmap, reg, &regval);
365 if (err < 0)
366 return err;
Eduardo Valentin22e73182013-07-16 14:54:55 -0400367
Guenter Roeck08b02432016-06-19 19:15:05 -0700368 *val = lm75_reg_to_mc(regval, data->resolution);
369 break;
370 default:
371 return -EINVAL;
372 }
Eduardo Valentin22e73182013-07-16 14:54:55 -0400373 return 0;
374}
375
Iker Perez del Palomar Sustatxa4b5be3c2019-08-08 09:02:46 +0100376static int lm75_write_temp(struct device *dev, u32 attr, long temp)
Jean Delvare9ca8e40c82007-05-08 17:22:01 +0200377{
Guenter Roecke65365f2016-06-19 17:49:19 -0700378 struct lm75_data *data = dev_get_drvdata(dev);
Jean Delvare87d06212013-05-04 14:49:36 +0200379 u8 resolution;
Guenter Roeck08b02432016-06-19 19:15:05 -0700380 int reg;
Shubhrajyoti De3cd9522010-10-28 20:31:44 +0200381
Guenter Roeck08b02432016-06-19 19:15:05 -0700382 switch (attr) {
383 case hwmon_temp_max:
384 reg = LM75_REG_MAX;
385 break;
386 case hwmon_temp_max_hyst:
387 reg = LM75_REG_HYST;
388 break;
389 default:
390 return -EINVAL;
391 }
Jean Delvare9ca8e40c82007-05-08 17:22:01 +0200392
Jean Delvare87d06212013-05-04 14:49:36 +0200393 /*
394 * Resolution of limit registers is assumed to be the same as the
395 * temperature input register resolution unless given explicitly.
396 */
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +0100397 if (data->params->resolution_limits)
398 resolution = data->params->resolution_limits;
Jean Delvare87d06212013-05-04 14:49:36 +0200399 else
400 resolution = data->resolution;
401
Jean Delvare87d06212013-05-04 14:49:36 +0200402 temp = clamp_val(temp, LM75_TEMP_MIN, LM75_TEMP_MAX);
Guenter Roecke65365f2016-06-19 17:49:19 -0700403 temp = DIV_ROUND_CLOSEST(temp << (resolution - 8),
404 1000) << (16 - resolution);
Guenter Roecke65365f2016-06-19 17:49:19 -0700405
Guenter Roeck7d82fcc2019-08-08 12:00:18 -0700406 return regmap_write(data->regmap, reg, (u16)temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Guenter Roeck040b1062019-08-08 14:30:23 -0700409static int lm75_update_interval(struct device *dev, long val)
Iker Perez del Palomar Sustatxa4b5be3c2019-08-08 09:02:46 +0100410{
411 struct lm75_data *data = dev_get_drvdata(dev);
Guenter Roeckcee04a02019-08-08 19:12:55 -0700412 unsigned int reg;
Iker Perez del Palomar Sustatxa4b5be3c2019-08-08 09:02:46 +0100413 u8 index;
414 s32 err;
415
Guenter Roeck040b1062019-08-08 14:30:23 -0700416 index = find_closest(val, data->params->sample_times,
417 (int)data->params->num_sample_times);
418
Guenter Roeckcee04a02019-08-08 19:12:55 -0700419 switch (data->kind) {
420 default:
421 err = lm75_write_config(data, lm75_sample_set_masks[index],
422 LM75_SAMPLE_CLEAR_MASK);
423 if (err)
424 return err;
Guenter Roeck040b1062019-08-08 14:30:23 -0700425
Guenter Roeckcee04a02019-08-08 19:12:55 -0700426 data->sample_time = data->params->sample_times[index];
427 if (data->params->resolutions)
428 data->resolution = data->params->resolutions[index];
429 break;
430 case tmp112:
431 err = regmap_read(data->regmap, LM75_REG_CONF, &reg);
432 if (err < 0)
433 return err;
434 reg &= ~0x00c0;
435 reg |= (3 - index) << 6;
436 err = regmap_write(data->regmap, LM75_REG_CONF, reg);
437 if (err < 0)
438 return err;
439 data->sample_time = data->params->sample_times[index];
440 break;
Guenter Roeckd7a85cd2019-08-08 19:28:51 -0700441 case pct2075:
442 err = i2c_smbus_write_byte_data(data->client, PCT2075_REG_IDLE,
443 index + 1);
444 if (err)
445 return err;
446 data->sample_time = data->params->sample_times[index];
447 break;
Guenter Roeckcee04a02019-08-08 19:12:55 -0700448 }
Guenter Roeck040b1062019-08-08 14:30:23 -0700449 return 0;
450}
451
452static int lm75_write_chip(struct device *dev, u32 attr, long val)
453{
Iker Perez del Palomar Sustatxa4b5be3c2019-08-08 09:02:46 +0100454 switch (attr) {
455 case hwmon_chip_update_interval:
Guenter Roeck040b1062019-08-08 14:30:23 -0700456 return lm75_update_interval(dev, val);
Iker Perez del Palomar Sustatxa4b5be3c2019-08-08 09:02:46 +0100457 default:
458 return -EINVAL;
459 }
460 return 0;
461}
462
463static int lm75_write(struct device *dev, enum hwmon_sensor_types type,
464 u32 attr, int channel, long val)
465{
466 switch (type) {
467 case hwmon_chip:
468 return lm75_write_chip(dev, attr, val);
469 case hwmon_temp:
470 return lm75_write_temp(dev, attr, val);
471 default:
472 return -EINVAL;
473 }
474 return 0;
475}
476
Guenter Roeck08b02432016-06-19 19:15:05 -0700477static umode_t lm75_is_visible(const void *data, enum hwmon_sensor_types type,
478 u32 attr, int channel)
Guenter Roeck5f7e5e22016-06-19 17:56:22 -0700479{
Iker Perez del Palomar Sustatxa4b5be3c2019-08-08 09:02:46 +0100480 const struct lm75_data *config_data = data;
481
Guenter Roeck08b02432016-06-19 19:15:05 -0700482 switch (type) {
483 case hwmon_chip:
484 switch (attr) {
485 case hwmon_chip_update_interval:
Iker Perez del Palomar Sustatxa4b5be3c2019-08-08 09:02:46 +0100486 if (config_data->params->num_sample_times > 1)
487 return 0644;
Guenter Roecke6ab6e02018-12-10 14:02:11 -0800488 return 0444;
Guenter Roeck08b02432016-06-19 19:15:05 -0700489 }
490 break;
491 case hwmon_temp:
492 switch (attr) {
493 case hwmon_temp_input:
Guenter Roecke6ab6e02018-12-10 14:02:11 -0800494 return 0444;
Guenter Roeck08b02432016-06-19 19:15:05 -0700495 case hwmon_temp_max:
496 case hwmon_temp_max_hyst:
Guenter Roecke6ab6e02018-12-10 14:02:11 -0800497 return 0644;
Guenter Roeck08b02432016-06-19 19:15:05 -0700498 }
499 break;
500 default:
501 break;
502 }
503 return 0;
Guenter Roeck5f7e5e22016-06-19 17:56:22 -0700504}
505
Guenter Roeck08b02432016-06-19 19:15:05 -0700506static const struct hwmon_channel_info *lm75_info[] = {
Guenter Roecke4f6fed12019-03-31 10:53:47 -0700507 HWMON_CHANNEL_INFO(chip,
508 HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL),
509 HWMON_CHANNEL_INFO(temp,
510 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST),
Guenter Roeck08b02432016-06-19 19:15:05 -0700511 NULL
512};
513
514static const struct hwmon_ops lm75_hwmon_ops = {
515 .is_visible = lm75_is_visible,
516 .read = lm75_read,
517 .write = lm75_write,
518};
519
520static const struct hwmon_chip_info lm75_chip_info = {
521 .ops = &lm75_hwmon_ops,
522 .info = lm75_info,
523};
524
Guenter Roecke65365f2016-06-19 17:49:19 -0700525static bool lm75_is_writeable_reg(struct device *dev, unsigned int reg)
526{
527 return reg != LM75_REG_TEMP;
528}
529
530static bool lm75_is_volatile_reg(struct device *dev, unsigned int reg)
531{
Guenter Roeckcee04a02019-08-08 19:12:55 -0700532 return reg == LM75_REG_TEMP || reg == LM75_REG_CONF;
Guenter Roecke65365f2016-06-19 17:49:19 -0700533}
534
535static const struct regmap_config lm75_regmap_config = {
536 .reg_bits = 8,
537 .val_bits = 16,
Guenter Roeckd7a85cd2019-08-08 19:28:51 -0700538 .max_register = PCT2075_REG_IDLE,
Guenter Roecke65365f2016-06-19 17:49:19 -0700539 .writeable_reg = lm75_is_writeable_reg,
540 .volatile_reg = lm75_is_volatile_reg,
541 .val_format_endian = REGMAP_ENDIAN_BIG,
542 .cache_type = REGCACHE_RBTREE,
David Frey1c96a2f2018-09-01 09:50:41 -0700543 .use_single_read = true,
544 .use_single_write = true,
Guenter Roecke65365f2016-06-19 17:49:19 -0700545};
546
Alban Bedel707d1512020-10-01 16:57:38 +0200547static void lm75_disable_regulator(void *data)
548{
549 struct lm75_data *lm75 = data;
550
551 regulator_disable(lm75->vs);
552}
553
Guenter Roeck9e37d3e2016-06-19 17:06:48 -0700554static void lm75_remove(void *data)
555{
556 struct lm75_data *lm75 = data;
557 struct i2c_client *client = lm75->client;
558
559 i2c_smbus_write_byte_data(client, LM75_REG_CONF, lm75->orig_conf);
560}
561
Stephen Kitt67487032020-08-13 18:02:22 +0200562static const struct i2c_device_id lm75_ids[];
563
564static int lm75_probe(struct i2c_client *client)
David Brownell9ebd3d82008-05-03 19:33:15 -0700565{
Guenter Roeckd663ec42014-01-13 16:00:37 -0800566 struct device *dev = &client->dev;
Guenter Roeck9e37d3e2016-06-19 17:06:48 -0700567 struct device *hwmon_dev;
David Brownell9ebd3d82008-05-03 19:33:15 -0700568 struct lm75_data *data;
Guenter Roeck90e2b542016-07-25 14:56:00 -0700569 int status, err;
Javier Martinez Canillase97a45f2017-02-24 10:13:02 -0300570 enum lm75_type kind;
571
572 if (client->dev.of_node)
573 kind = (enum lm75_type)of_device_get_match_data(&client->dev);
574 else
Stephen Kitt67487032020-08-13 18:02:22 +0200575 kind = i2c_match_id(lm75_ids, client)->driver_data;
David Brownell9ebd3d82008-05-03 19:33:15 -0700576
577 if (!i2c_check_functionality(client->adapter,
578 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
579 return -EIO;
580
Guenter Roeckd663ec42014-01-13 16:00:37 -0800581 data = devm_kzalloc(dev, sizeof(struct lm75_data), GFP_KERNEL);
David Brownell9ebd3d82008-05-03 19:33:15 -0700582 if (!data)
583 return -ENOMEM;
584
Guenter Roeckd663ec42014-01-13 16:00:37 -0800585 data->client = client;
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +0100586 data->kind = kind;
Guenter Roecke65365f2016-06-19 17:49:19 -0700587
Alban Bedel707d1512020-10-01 16:57:38 +0200588 data->vs = devm_regulator_get(dev, "vs");
589 if (IS_ERR(data->vs))
590 return PTR_ERR(data->vs);
591
Guenter Roecke65365f2016-06-19 17:49:19 -0700592 data->regmap = devm_regmap_init_i2c(client, &lm75_regmap_config);
593 if (IS_ERR(data->regmap))
594 return PTR_ERR(data->regmap);
David Brownell9ebd3d82008-05-03 19:33:15 -0700595
596 /* Set to LM75 resolution (9 bits, 1/2 degree C) and range.
597 * Then tweak to be more precise when appropriate.
598 */
Jean Delvare8a5c5cc2013-05-04 14:49:36 +0200599
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +0100600 data->params = &device_params[data->kind];
David Brownell9ebd3d82008-05-03 19:33:15 -0700601
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +0100602 /* Save default sample time and resolution*/
603 data->sample_time = data->params->default_sample_time;
604 data->resolution = data->params->default_resolution;
605
Alban Bedel707d1512020-10-01 16:57:38 +0200606 /* Enable the power */
607 err = regulator_enable(data->vs);
608 if (err) {
609 dev_err(dev, "failed to enable regulator: %d\n", err);
610 return err;
611 }
612
613 err = devm_add_action_or_reset(dev, lm75_disable_regulator, data);
614 if (err)
615 return err;
616
Iker Perez del Palomar Sustatxadcb12652019-08-08 09:02:43 +0100617 /* Cache original configuration */
Guenter Roeck38aefb42016-06-19 17:11:13 -0700618 status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
David Brownell9ebd3d82008-05-03 19:33:15 -0700619 if (status < 0) {
Guenter Roeckd663ec42014-01-13 16:00:37 -0800620 dev_dbg(dev, "Can't read config? %d\n", status);
Guenter Roeck13ac7a02012-06-02 09:58:08 -0700621 return status;
David Brownell9ebd3d82008-05-03 19:33:15 -0700622 }
623 data->orig_conf = status;
Iker Perez del Palomar Sustatxa58608cf2019-08-08 09:02:44 +0100624 data->current_conf = status;
625
626 err = lm75_write_config(data, data->params->set_mask,
627 data->params->clr_mask);
628 if (err)
629 return err;
Guenter Roeck9e37d3e2016-06-19 17:06:48 -0700630
Guenter Roeck90e2b542016-07-25 14:56:00 -0700631 err = devm_add_action_or_reset(dev, lm75_remove, data);
632 if (err)
633 return err;
Guenter Roeck9e37d3e2016-06-19 17:06:48 -0700634
Guenter Roeck08b02432016-06-19 19:15:05 -0700635 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
636 data, &lm75_chip_info,
637 NULL);
Guenter Roeck9e37d3e2016-06-19 17:06:48 -0700638 if (IS_ERR(hwmon_dev))
639 return PTR_ERR(hwmon_dev);
David Brownell9ebd3d82008-05-03 19:33:15 -0700640
Guenter Roeck9e37d3e2016-06-19 17:06:48 -0700641 dev_info(dev, "%s: sensor '%s'\n", dev_name(hwmon_dev), client->name);
David Brownell9ebd3d82008-05-03 19:33:15 -0700642
643 return 0;
David Brownell9ebd3d82008-05-03 19:33:15 -0700644}
645
David Brownell9ebd3d82008-05-03 19:33:15 -0700646static const struct i2c_device_id lm75_ids[] = {
Michael Henneriche96f9d82011-10-13 04:43:31 -0400647 { "adt75", adt75, },
David Brownell9ebd3d82008-05-03 19:33:15 -0700648 { "ds1775", ds1775, },
649 { "ds75", ds75, },
Jean Delvare3fbc81e2013-05-04 14:49:36 +0200650 { "ds7505", ds7505, },
Arnaud Ebalardc98d6c62013-11-09 18:39:14 +0100651 { "g751", g751, },
David Brownell9ebd3d82008-05-03 19:33:15 -0700652 { "lm75", lm75, },
653 { "lm75a", lm75a, },
Michael Thalmeier799fc602014-11-18 17:08:04 +0100654 { "lm75b", lm75b, },
David Brownell9ebd3d82008-05-03 19:33:15 -0700655 { "max6625", max6625, },
656 { "max6626", max6626, },
Kun Yia54ca772018-09-11 13:25:58 -0700657 { "max31725", max31725, },
658 { "max31726", max31725, },
David Brownell9ebd3d82008-05-03 19:33:15 -0700659 { "mcp980x", mcp980x, },
Daniel Mack557c7ff2019-07-11 14:45:04 +0200660 { "pct2075", pct2075, },
David Brownell9ebd3d82008-05-03 19:33:15 -0700661 { "stds75", stds75, },
Jagan Teki2e9a41b2018-12-06 02:44:22 +0530662 { "stlm75", stlm75, },
David Brownell9ebd3d82008-05-03 19:33:15 -0700663 { "tcn75", tcn75, },
664 { "tmp100", tmp100, },
665 { "tmp101", tmp101, },
Shubhrajyoti Datta6d034052010-05-27 19:59:03 +0200666 { "tmp105", tmp105, },
Frans Klaverc83959f2014-06-26 11:21:11 +0200667 { "tmp112", tmp112, },
David Brownell9ebd3d82008-05-03 19:33:15 -0700668 { "tmp175", tmp175, },
669 { "tmp275", tmp275, },
670 { "tmp75", tmp75, },
Iker Perez del Palomar Sustatxa39abe9d2019-05-03 17:15:00 +0100671 { "tmp75b", tmp75b, },
Ben Gardner9c32e812015-10-07 21:55:20 -0500672 { "tmp75c", tmp75c, },
Robert Markoec081f92021-04-29 14:11:49 +0200673 { "tmp1075", tmp1075, },
David Brownell9ebd3d82008-05-03 19:33:15 -0700674 { /* LIST END */ }
675};
676MODULE_DEVICE_TABLE(i2c, lm75_ids);
677
Guenter Roeckffa83e72019-04-04 06:40:00 -0700678static const struct of_device_id __maybe_unused lm75_of_match[] = {
Javier Martinez Canillase97a45f2017-02-24 10:13:02 -0300679 {
680 .compatible = "adi,adt75",
681 .data = (void *)adt75
682 },
683 {
684 .compatible = "dallas,ds1775",
685 .data = (void *)ds1775
686 },
687 {
688 .compatible = "dallas,ds75",
689 .data = (void *)ds75
690 },
691 {
692 .compatible = "dallas,ds7505",
693 .data = (void *)ds7505
694 },
695 {
696 .compatible = "gmt,g751",
697 .data = (void *)g751
698 },
699 {
700 .compatible = "national,lm75",
701 .data = (void *)lm75
702 },
703 {
704 .compatible = "national,lm75a",
705 .data = (void *)lm75a
706 },
707 {
708 .compatible = "national,lm75b",
709 .data = (void *)lm75b
710 },
711 {
712 .compatible = "maxim,max6625",
713 .data = (void *)max6625
714 },
715 {
716 .compatible = "maxim,max6626",
717 .data = (void *)max6626
718 },
719 {
Kun Yia54ca772018-09-11 13:25:58 -0700720 .compatible = "maxim,max31725",
721 .data = (void *)max31725
722 },
723 {
724 .compatible = "maxim,max31726",
725 .data = (void *)max31725
726 },
727 {
Javier Martinez Canillase97a45f2017-02-24 10:13:02 -0300728 .compatible = "maxim,mcp980x",
729 .data = (void *)mcp980x
730 },
731 {
Daniel Mack557c7ff2019-07-11 14:45:04 +0200732 .compatible = "nxp,pct2075",
733 .data = (void *)pct2075
734 },
735 {
Javier Martinez Canillase97a45f2017-02-24 10:13:02 -0300736 .compatible = "st,stds75",
737 .data = (void *)stds75
738 },
739 {
Jagan Teki2e9a41b2018-12-06 02:44:22 +0530740 .compatible = "st,stlm75",
741 .data = (void *)stlm75
742 },
743 {
Javier Martinez Canillase97a45f2017-02-24 10:13:02 -0300744 .compatible = "microchip,tcn75",
745 .data = (void *)tcn75
746 },
747 {
748 .compatible = "ti,tmp100",
749 .data = (void *)tmp100
750 },
751 {
752 .compatible = "ti,tmp101",
753 .data = (void *)tmp101
754 },
755 {
756 .compatible = "ti,tmp105",
757 .data = (void *)tmp105
758 },
759 {
760 .compatible = "ti,tmp112",
761 .data = (void *)tmp112
762 },
763 {
764 .compatible = "ti,tmp175",
765 .data = (void *)tmp175
766 },
767 {
768 .compatible = "ti,tmp275",
769 .data = (void *)tmp275
770 },
771 {
772 .compatible = "ti,tmp75",
773 .data = (void *)tmp75
774 },
775 {
Iker Perez del Palomar Sustatxa39abe9d2019-05-03 17:15:00 +0100776 .compatible = "ti,tmp75b",
777 .data = (void *)tmp75b
778 },
779 {
Javier Martinez Canillase97a45f2017-02-24 10:13:02 -0300780 .compatible = "ti,tmp75c",
781 .data = (void *)tmp75c
782 },
Robert Markoec081f92021-04-29 14:11:49 +0200783 {
784 .compatible = "ti,tmp1075",
785 .data = (void *)tmp1075
786 },
Javier Martinez Canillase97a45f2017-02-24 10:13:02 -0300787 { },
788};
789MODULE_DEVICE_TABLE(of, lm75_of_match);
790
Len Sorensen05e82fe2011-03-21 17:59:36 +0100791#define LM75A_ID 0xA1
792
Jean Delvare8ff69ee2008-08-10 22:56:16 +0200793/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100794static int lm75_detect(struct i2c_client *new_client,
Jean Delvare8ff69ee2008-08-10 22:56:16 +0200795 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Jean Delvare8ff69ee2008-08-10 22:56:16 +0200797 struct i2c_adapter *adapter = new_client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 int i;
Jean Delvaree76f67b2011-03-21 17:59:36 +0100799 int conf, hyst, os;
Len Sorensen05e82fe2011-03-21 17:59:36 +0100800 bool is_lm75a = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
803 I2C_FUNC_SMBUS_WORD_DATA))
Jean Delvare8ff69ee2008-08-10 22:56:16 +0200804 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Jean Delvare426343e2011-10-13 17:15:11 -0400806 /*
807 * Now, we do the remaining detection. There is no identification-
808 * dedicated register so we have to rely on several tricks:
809 * unused bits, registers cycling over 8-address boundaries,
810 * addresses 0x04-0x07 returning the last read value.
811 * The cycling+unused addresses combination is not tested,
812 * since it would significantly slow the detection down and would
813 * hardly add any value.
814 *
815 * The National Semiconductor LM75A is different than earlier
816 * LM75s. It has an ID byte of 0xaX (where X is the chip
817 * revision, with 1 being the only revision in existence) in
818 * register 7, and unused registers return 0xff rather than the
819 * last read value.
820 *
821 * Note that this function only detects the original National
822 * Semiconductor LM75 and the LM75A. Clones from other vendors
823 * aren't detected, on purpose, because they are typically never
824 * found on PC hardware. They are found on embedded designs where
825 * they can be instantiated explicitly so detection is not needed.
826 * The absence of identification registers on all these clones
827 * would make their exhaustive detection very difficult and weak,
828 * and odds are that the driver would bind to unsupported devices.
829 */
Len Sorensen05e82fe2011-03-21 17:59:36 +0100830
Jean Delvaree76f67b2011-03-21 17:59:36 +0100831 /* Unused bits */
Jean Delvare52df6442009-12-09 20:35:57 +0100832 conf = i2c_smbus_read_byte_data(new_client, 1);
Jean Delvaree76f67b2011-03-21 17:59:36 +0100833 if (conf & 0xe0)
834 return -ENODEV;
Len Sorensen05e82fe2011-03-21 17:59:36 +0100835
836 /* First check for LM75A */
837 if (i2c_smbus_read_byte_data(new_client, 7) == LM75A_ID) {
Michal Orzel8cbf2172020-04-30 16:05:34 +0200838 /*
839 * LM75A returns 0xff on unused registers so
840 * just to be sure we check for that too.
841 */
Len Sorensen05e82fe2011-03-21 17:59:36 +0100842 if (i2c_smbus_read_byte_data(new_client, 4) != 0xff
843 || i2c_smbus_read_byte_data(new_client, 5) != 0xff
844 || i2c_smbus_read_byte_data(new_client, 6) != 0xff)
845 return -ENODEV;
846 is_lm75a = 1;
Jean Delvaree76f67b2011-03-21 17:59:36 +0100847 hyst = i2c_smbus_read_byte_data(new_client, 2);
848 os = i2c_smbus_read_byte_data(new_client, 3);
Len Sorensen05e82fe2011-03-21 17:59:36 +0100849 } else { /* Traditional style LM75 detection */
850 /* Unused addresses */
Jean Delvaree76f67b2011-03-21 17:59:36 +0100851 hyst = i2c_smbus_read_byte_data(new_client, 2);
852 if (i2c_smbus_read_byte_data(new_client, 4) != hyst
853 || i2c_smbus_read_byte_data(new_client, 5) != hyst
854 || i2c_smbus_read_byte_data(new_client, 6) != hyst
855 || i2c_smbus_read_byte_data(new_client, 7) != hyst)
Len Sorensen05e82fe2011-03-21 17:59:36 +0100856 return -ENODEV;
Jean Delvaree76f67b2011-03-21 17:59:36 +0100857 os = i2c_smbus_read_byte_data(new_client, 3);
858 if (i2c_smbus_read_byte_data(new_client, 4) != os
859 || i2c_smbus_read_byte_data(new_client, 5) != os
860 || i2c_smbus_read_byte_data(new_client, 6) != os
861 || i2c_smbus_read_byte_data(new_client, 7) != os)
Len Sorensen05e82fe2011-03-21 17:59:36 +0100862 return -ENODEV;
863 }
Guenter Roeck4ad40cc2014-12-04 09:58:15 -0800864 /*
865 * It is very unlikely that this is a LM75 if both
866 * hysteresis and temperature limit registers are 0.
867 */
868 if (hyst == 0 && os == 0)
869 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Jean Delvare52df6442009-12-09 20:35:57 +0100871 /* Addresses cycling */
Jean Delvaree76f67b2011-03-21 17:59:36 +0100872 for (i = 8; i <= 248; i += 40) {
Jean Delvare52df6442009-12-09 20:35:57 +0100873 if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
Jean Delvaree76f67b2011-03-21 17:59:36 +0100874 || i2c_smbus_read_byte_data(new_client, i + 2) != hyst
875 || i2c_smbus_read_byte_data(new_client, i + 3) != os)
Jean Delvare52df6442009-12-09 20:35:57 +0100876 return -ENODEV;
Len Sorensen05e82fe2011-03-21 17:59:36 +0100877 if (is_lm75a && i2c_smbus_read_byte_data(new_client, i + 7)
878 != LM75A_ID)
879 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
881
Len Sorensen05e82fe2011-03-21 17:59:36 +0100882 strlcpy(info->type, is_lm75a ? "lm75a" : "lm75", I2C_NAME_SIZE);
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
886
Shubhrajyoti Datta99145182010-08-14 21:08:50 +0200887#ifdef CONFIG_PM
888static int lm75_suspend(struct device *dev)
889{
890 int status;
891 struct i2c_client *client = to_i2c_client(dev);
Michal Orzel8cbf2172020-04-30 16:05:34 +0200892
Guenter Roeck38aefb42016-06-19 17:11:13 -0700893 status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
Shubhrajyoti Datta99145182010-08-14 21:08:50 +0200894 if (status < 0) {
895 dev_dbg(&client->dev, "Can't read config? %d\n", status);
896 return status;
897 }
898 status = status | LM75_SHUTDOWN;
Guenter Roeck38aefb42016-06-19 17:11:13 -0700899 i2c_smbus_write_byte_data(client, LM75_REG_CONF, status);
Shubhrajyoti Datta99145182010-08-14 21:08:50 +0200900 return 0;
901}
902
903static int lm75_resume(struct device *dev)
904{
905 int status;
906 struct i2c_client *client = to_i2c_client(dev);
Michal Orzel8cbf2172020-04-30 16:05:34 +0200907
Guenter Roeck38aefb42016-06-19 17:11:13 -0700908 status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
Shubhrajyoti Datta99145182010-08-14 21:08:50 +0200909 if (status < 0) {
910 dev_dbg(&client->dev, "Can't read config? %d\n", status);
911 return status;
912 }
913 status = status & ~LM75_SHUTDOWN;
Guenter Roeck38aefb42016-06-19 17:11:13 -0700914 i2c_smbus_write_byte_data(client, LM75_REG_CONF, status);
Shubhrajyoti Datta99145182010-08-14 21:08:50 +0200915 return 0;
916}
917
918static const struct dev_pm_ops lm75_dev_pm_ops = {
919 .suspend = lm75_suspend,
920 .resume = lm75_resume,
921};
922#define LM75_DEV_PM_OPS (&lm75_dev_pm_ops)
923#else
924#define LM75_DEV_PM_OPS NULL
925#endif /* CONFIG_PM */
926
Jean Delvare8ff69ee2008-08-10 22:56:16 +0200927static struct i2c_driver lm75_driver = {
928 .class = I2C_CLASS_HWMON,
David Brownell01a52392008-04-21 12:10:53 -0700929 .driver = {
Jean Delvare8ff69ee2008-08-10 22:56:16 +0200930 .name = "lm75",
Javier Martinez Canillase97a45f2017-02-24 10:13:02 -0300931 .of_match_table = of_match_ptr(lm75_of_match),
Shubhrajyoti Datta99145182010-08-14 21:08:50 +0200932 .pm = LM75_DEV_PM_OPS,
David Brownell01a52392008-04-21 12:10:53 -0700933 },
Stephen Kitt67487032020-08-13 18:02:22 +0200934 .probe_new = lm75_probe,
Jean Delvare8ff69ee2008-08-10 22:56:16 +0200935 .id_table = lm75_ids,
936 .detect = lm75_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100937 .address_list = normal_i2c,
David Brownell01a52392008-04-21 12:10:53 -0700938};
939
Axel Linf0967ee2012-01-20 15:38:18 +0800940module_i2c_driver(lm75_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
943MODULE_DESCRIPTION("LM75 driver");
944MODULE_LICENSE("GPL");