Linux Audio

Check our new training course

Loading...
v5.9
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * lm90.c - Part of lm_sensors, Linux kernel modules for hardware
   4 *          monitoring
   5 * Copyright (C) 2003-2010  Jean Delvare <jdelvare@suse.de>
   6 *
   7 * Based on the lm83 driver. The LM90 is a sensor chip made by National
   8 * Semiconductor. It reports up to two temperatures (its own plus up to
   9 * one external one) with a 0.125 deg resolution (1 deg for local
  10 * temperature) and a 3-4 deg accuracy.
  11 *
  12 * This driver also supports the LM89 and LM99, two other sensor chips
  13 * made by National Semiconductor. Both have an increased remote
  14 * temperature measurement accuracy (1 degree), and the LM99
  15 * additionally shifts remote temperatures (measured and limits) by 16
  16 * degrees, which allows for higher temperatures measurement.
  17 * Note that there is no way to differentiate between both chips.
  18 * When device is auto-detected, the driver will assume an LM99.
  19 *
  20 * This driver also supports the LM86, another sensor chip made by
  21 * National Semiconductor. It is exactly similar to the LM90 except it
  22 * has a higher accuracy.
  23 *
  24 * This driver also supports the ADM1032, a sensor chip made by Analog
  25 * Devices. That chip is similar to the LM90, with a few differences
  26 * that are not handled by this driver. Among others, it has a higher
  27 * accuracy than the LM90, much like the LM86 does.
  28 *
  29 * This driver also supports the MAX6657, MAX6658 and MAX6659 sensor
  30 * chips made by Maxim. These chips are similar to the LM86.
  31 * Note that there is no easy way to differentiate between the three
  32 * variants. We use the device address to detect MAX6659, which will result
  33 * in a detection as max6657 if it is on address 0x4c. The extra address
  34 * and features of the MAX6659 are only supported if the chip is configured
  35 * explicitly as max6659, or if its address is not 0x4c.
  36 * These chips lack the remote temperature offset feature.
  37 *
  38 * This driver also supports the MAX6654 chip made by Maxim. This chip can
  39 * be at 9 different addresses, similar to MAX6680/MAX6681. The MAX6654 is
  40 * otherwise similar to MAX6657/MAX6658/MAX6659. Extended range is available
  41 * by setting the configuration register accordingly, and is done during
  42 * initialization. Extended precision is only available at conversion rates
  43 * of 1 Hz and slower. Note that extended precision is not enabled by
  44 * default, as this driver initializes all chips to 2 Hz by design.
  45 *
  46 * This driver also supports the MAX6646, MAX6647, MAX6648, MAX6649 and
  47 * MAX6692 chips made by Maxim.  These are again similar to the LM86,
  48 * but they use unsigned temperature values and can report temperatures
  49 * from 0 to 145 degrees.
  50 *
  51 * This driver also supports the MAX6680 and MAX6681, two other sensor
  52 * chips made by Maxim. These are quite similar to the other Maxim
  53 * chips. The MAX6680 and MAX6681 only differ in the pinout so they can
  54 * be treated identically.
  55 *
  56 * This driver also supports the MAX6695 and MAX6696, two other sensor
  57 * chips made by Maxim. These are also quite similar to other Maxim
  58 * chips, but support three temperature sensors instead of two. MAX6695
  59 * and MAX6696 only differ in the pinout so they can be treated identically.
  60 *
  61 * This driver also supports ADT7461 and ADT7461A from Analog Devices as well as
  62 * NCT1008 from ON Semiconductor. The chips are supported in both compatibility
  63 * and extended mode. They are mostly compatible with LM90 except for a data
  64 * format difference for the temperature value registers.
  65 *
  66 * This driver also supports the SA56004 from Philips. This device is
  67 * pin-compatible with the LM86, the ED/EDP parts are also address-compatible.
  68 *
  69 * This driver also supports the G781 from GMT. This device is compatible
  70 * with the ADM1032.
  71 *
  72 * This driver also supports TMP451 from Texas Instruments. This device is
  73 * supported in both compatibility and extended mode. It's mostly compatible
  74 * with ADT7461 except for local temperature low byte register and max
  75 * conversion rate.
  76 *
  77 * Since the LM90 was the first chipset supported by this driver, most
  78 * comments will refer to this chipset, but are actually general and
  79 * concern all supported chipsets, unless mentioned otherwise.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  80 */
  81
  82#include <linux/module.h>
  83#include <linux/init.h>
  84#include <linux/slab.h>
  85#include <linux/jiffies.h>
  86#include <linux/i2c.h>
 
  87#include <linux/hwmon.h>
  88#include <linux/err.h>
  89#include <linux/mutex.h>
  90#include <linux/of_device.h>
  91#include <linux/sysfs.h>
  92#include <linux/interrupt.h>
  93#include <linux/regulator/consumer.h>
  94
  95/*
  96 * Addresses to scan
  97 * Address is fully defined internally and cannot be changed except for
  98 * MAX6659, MAX6680 and MAX6681.
  99 * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, ADT7461A, MAX6649,
 100 * MAX6657, MAX6658, NCT1008 and W83L771 have address 0x4c.
 101 * ADM1032-2, ADT7461-2, ADT7461A-2, LM89-1, LM99-1, MAX6646, and NCT1008D
 102 * have address 0x4d.
 103 * MAX6647 has address 0x4e.
 104 * MAX6659 can have address 0x4c, 0x4d or 0x4e.
 105 * MAX6654, MAX6680, and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29,
 106 * 0x2a, 0x2b, 0x4c, 0x4d or 0x4e.
 107 * SA56004 can have address 0x48 through 0x4F.
 108 */
 109
 110static const unsigned short normal_i2c[] = {
 111	0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
 112	0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
 113
 114enum chips { lm90, adm1032, lm99, lm86, max6657, max6659, adt7461, max6680,
 115	max6646, w83l771, max6696, sa56004, g781, tmp451, max6654 };
 116
 117/*
 118 * The LM90 registers
 119 */
 120
 121#define LM90_REG_R_MAN_ID		0xFE
 122#define LM90_REG_R_CHIP_ID		0xFF
 123#define LM90_REG_R_CONFIG1		0x03
 124#define LM90_REG_W_CONFIG1		0x09
 125#define LM90_REG_R_CONFIG2		0xBF
 126#define LM90_REG_W_CONFIG2		0xBF
 127#define LM90_REG_R_CONVRATE		0x04
 128#define LM90_REG_W_CONVRATE		0x0A
 129#define LM90_REG_R_STATUS		0x02
 130#define LM90_REG_R_LOCAL_TEMP		0x00
 131#define LM90_REG_R_LOCAL_HIGH		0x05
 132#define LM90_REG_W_LOCAL_HIGH		0x0B
 133#define LM90_REG_R_LOCAL_LOW		0x06
 134#define LM90_REG_W_LOCAL_LOW		0x0C
 135#define LM90_REG_R_LOCAL_CRIT		0x20
 136#define LM90_REG_W_LOCAL_CRIT		0x20
 137#define LM90_REG_R_REMOTE_TEMPH		0x01
 138#define LM90_REG_R_REMOTE_TEMPL		0x10
 139#define LM90_REG_R_REMOTE_OFFSH		0x11
 140#define LM90_REG_W_REMOTE_OFFSH		0x11
 141#define LM90_REG_R_REMOTE_OFFSL		0x12
 142#define LM90_REG_W_REMOTE_OFFSL		0x12
 143#define LM90_REG_R_REMOTE_HIGHH		0x07
 144#define LM90_REG_W_REMOTE_HIGHH		0x0D
 145#define LM90_REG_R_REMOTE_HIGHL		0x13
 146#define LM90_REG_W_REMOTE_HIGHL		0x13
 147#define LM90_REG_R_REMOTE_LOWH		0x08
 148#define LM90_REG_W_REMOTE_LOWH		0x0E
 149#define LM90_REG_R_REMOTE_LOWL		0x14
 150#define LM90_REG_W_REMOTE_LOWL		0x14
 151#define LM90_REG_R_REMOTE_CRIT		0x19
 152#define LM90_REG_W_REMOTE_CRIT		0x19
 153#define LM90_REG_R_TCRIT_HYST		0x21
 154#define LM90_REG_W_TCRIT_HYST		0x21
 155
 156/* MAX6646/6647/6649/6654/6657/6658/6659/6695/6696 registers */
 157
 158#define MAX6657_REG_R_LOCAL_TEMPL	0x11
 159#define MAX6696_REG_R_STATUS2		0x12
 160#define MAX6659_REG_R_REMOTE_EMERG	0x16
 161#define MAX6659_REG_W_REMOTE_EMERG	0x16
 162#define MAX6659_REG_R_LOCAL_EMERG	0x17
 163#define MAX6659_REG_W_LOCAL_EMERG	0x17
 164
 165/*  SA56004 registers */
 166
 167#define SA56004_REG_R_LOCAL_TEMPL 0x22
 168
 
 169#define LM90_MAX_CONVRATE_MS	16000	/* Maximum conversion rate in ms */
 170
 171/* TMP451 registers */
 172#define TMP451_REG_R_LOCAL_TEMPL	0x15
 173
 174/*
 175 * Device flags
 176 */
 177#define LM90_FLAG_ADT7461_EXT	(1 << 0) /* ADT7461 extended mode	*/
 178/* Device features */
 179#define LM90_HAVE_OFFSET	(1 << 1) /* temperature offset register	*/
 180#define LM90_HAVE_REM_LIMIT_EXT	(1 << 3) /* extended remote limit	*/
 181#define LM90_HAVE_EMERGENCY	(1 << 4) /* 3rd upper (emergency) limit	*/
 182#define LM90_HAVE_EMERGENCY_ALARM (1 << 5)/* emergency alarm		*/
 183#define LM90_HAVE_TEMP3		(1 << 6) /* 3rd temperature sensor	*/
 184#define LM90_HAVE_BROKEN_ALERT	(1 << 7) /* Broken alert		*/
 185#define LM90_PAUSE_FOR_CONFIG	(1 << 8) /* Pause conversion for config	*/
 186
 187/* LM90 status */
 188#define LM90_STATUS_LTHRM	(1 << 0) /* local THERM limit tripped */
 189#define LM90_STATUS_RTHRM	(1 << 1) /* remote THERM limit tripped */
 190#define LM90_STATUS_ROPEN	(1 << 2) /* remote is an open circuit */
 191#define LM90_STATUS_RLOW	(1 << 3) /* remote low temp limit tripped */
 192#define LM90_STATUS_RHIGH	(1 << 4) /* remote high temp limit tripped */
 193#define LM90_STATUS_LLOW	(1 << 5) /* local low temp limit tripped */
 194#define LM90_STATUS_LHIGH	(1 << 6) /* local high temp limit tripped */
 195
 196#define MAX6696_STATUS2_R2THRM	(1 << 1) /* remote2 THERM limit tripped */
 197#define MAX6696_STATUS2_R2OPEN	(1 << 2) /* remote2 is an open circuit */
 198#define MAX6696_STATUS2_R2LOW	(1 << 3) /* remote2 low temp limit tripped */
 199#define MAX6696_STATUS2_R2HIGH	(1 << 4) /* remote2 high temp limit tripped */
 200#define MAX6696_STATUS2_ROT2	(1 << 5) /* remote emergency limit tripped */
 201#define MAX6696_STATUS2_R2OT2	(1 << 6) /* remote2 emergency limit tripped */
 202#define MAX6696_STATUS2_LOT2	(1 << 7) /* local emergency limit tripped */
 203
 204/*
 205 * Driver data (common to all clients)
 206 */
 207
 208static const struct i2c_device_id lm90_id[] = {
 209	{ "adm1032", adm1032 },
 210	{ "adt7461", adt7461 },
 211	{ "adt7461a", adt7461 },
 212	{ "g781", g781 },
 213	{ "lm90", lm90 },
 214	{ "lm86", lm86 },
 215	{ "lm89", lm86 },
 216	{ "lm99", lm99 },
 217	{ "max6646", max6646 },
 218	{ "max6647", max6646 },
 219	{ "max6649", max6646 },
 220	{ "max6654", max6654 },
 221	{ "max6657", max6657 },
 222	{ "max6658", max6657 },
 223	{ "max6659", max6659 },
 224	{ "max6680", max6680 },
 225	{ "max6681", max6680 },
 226	{ "max6695", max6696 },
 227	{ "max6696", max6696 },
 228	{ "nct1008", adt7461 },
 229	{ "w83l771", w83l771 },
 230	{ "sa56004", sa56004 },
 231	{ "tmp451", tmp451 },
 232	{ }
 233};
 234MODULE_DEVICE_TABLE(i2c, lm90_id);
 235
 236static const struct of_device_id __maybe_unused lm90_of_match[] = {
 237	{
 238		.compatible = "adi,adm1032",
 239		.data = (void *)adm1032
 240	},
 241	{
 242		.compatible = "adi,adt7461",
 243		.data = (void *)adt7461
 244	},
 245	{
 246		.compatible = "adi,adt7461a",
 247		.data = (void *)adt7461
 248	},
 249	{
 250		.compatible = "gmt,g781",
 251		.data = (void *)g781
 252	},
 253	{
 254		.compatible = "national,lm90",
 255		.data = (void *)lm90
 256	},
 257	{
 258		.compatible = "national,lm86",
 259		.data = (void *)lm86
 260	},
 261	{
 262		.compatible = "national,lm89",
 263		.data = (void *)lm86
 264	},
 265	{
 266		.compatible = "national,lm99",
 267		.data = (void *)lm99
 268	},
 269	{
 270		.compatible = "dallas,max6646",
 271		.data = (void *)max6646
 272	},
 273	{
 274		.compatible = "dallas,max6647",
 275		.data = (void *)max6646
 276	},
 277	{
 278		.compatible = "dallas,max6649",
 279		.data = (void *)max6646
 280	},
 281	{
 282		.compatible = "dallas,max6654",
 283		.data = (void *)max6654
 284	},
 285	{
 286		.compatible = "dallas,max6657",
 287		.data = (void *)max6657
 288	},
 289	{
 290		.compatible = "dallas,max6658",
 291		.data = (void *)max6657
 292	},
 293	{
 294		.compatible = "dallas,max6659",
 295		.data = (void *)max6659
 296	},
 297	{
 298		.compatible = "dallas,max6680",
 299		.data = (void *)max6680
 300	},
 301	{
 302		.compatible = "dallas,max6681",
 303		.data = (void *)max6680
 304	},
 305	{
 306		.compatible = "dallas,max6695",
 307		.data = (void *)max6696
 308	},
 309	{
 310		.compatible = "dallas,max6696",
 311		.data = (void *)max6696
 312	},
 313	{
 314		.compatible = "onnn,nct1008",
 315		.data = (void *)adt7461
 316	},
 317	{
 318		.compatible = "winbond,w83l771",
 319		.data = (void *)w83l771
 320	},
 321	{
 322		.compatible = "nxp,sa56004",
 323		.data = (void *)sa56004
 324	},
 325	{
 326		.compatible = "ti,tmp451",
 327		.data = (void *)tmp451
 328	},
 329	{ },
 330};
 331MODULE_DEVICE_TABLE(of, lm90_of_match);
 332
 333/*
 334 * chip type specific parameters
 335 */
 336struct lm90_params {
 337	u32 flags;		/* Capabilities */
 338	u16 alert_alarms;	/* Which alarm bits trigger ALERT# */
 339				/* Upper 8 bits for max6695/96 */
 340	u8 max_convrate;	/* Maximum conversion rate register value */
 341	u8 reg_local_ext;	/* Extended local temp register (optional) */
 342};
 343
 344static const struct lm90_params lm90_params[] = {
 345	[adm1032] = {
 346		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
 347		  | LM90_HAVE_BROKEN_ALERT,
 348		.alert_alarms = 0x7c,
 349		.max_convrate = 10,
 350	},
 351	[adt7461] = {
 352		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
 353		  | LM90_HAVE_BROKEN_ALERT,
 354		.alert_alarms = 0x7c,
 355		.max_convrate = 10,
 356	},
 357	[g781] = {
 358		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
 359		  | LM90_HAVE_BROKEN_ALERT,
 360		.alert_alarms = 0x7c,
 361		.max_convrate = 8,
 362	},
 363	[lm86] = {
 364		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
 365		.alert_alarms = 0x7b,
 366		.max_convrate = 9,
 367	},
 368	[lm90] = {
 369		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
 370		.alert_alarms = 0x7b,
 371		.max_convrate = 9,
 372	},
 373	[lm99] = {
 374		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
 375		.alert_alarms = 0x7b,
 376		.max_convrate = 9,
 377	},
 378	[max6646] = {
 379		.alert_alarms = 0x7c,
 380		.max_convrate = 6,
 381		.reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
 382	},
 383	[max6654] = {
 384		.alert_alarms = 0x7c,
 385		.max_convrate = 7,
 386		.reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
 387	},
 388	[max6657] = {
 389		.flags = LM90_PAUSE_FOR_CONFIG,
 390		.alert_alarms = 0x7c,
 391		.max_convrate = 8,
 392		.reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
 393	},
 394	[max6659] = {
 395		.flags = LM90_HAVE_EMERGENCY,
 396		.alert_alarms = 0x7c,
 397		.max_convrate = 8,
 398		.reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
 399	},
 400	[max6680] = {
 401		.flags = LM90_HAVE_OFFSET,
 402		.alert_alarms = 0x7c,
 403		.max_convrate = 7,
 404	},
 405	[max6696] = {
 406		.flags = LM90_HAVE_EMERGENCY
 407		  | LM90_HAVE_EMERGENCY_ALARM | LM90_HAVE_TEMP3,
 408		.alert_alarms = 0x1c7c,
 409		.max_convrate = 6,
 410		.reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
 411	},
 412	[w83l771] = {
 413		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
 414		.alert_alarms = 0x7c,
 415		.max_convrate = 8,
 416	},
 417	[sa56004] = {
 418		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
 419		.alert_alarms = 0x7b,
 420		.max_convrate = 9,
 421		.reg_local_ext = SA56004_REG_R_LOCAL_TEMPL,
 422	},
 423	[tmp451] = {
 424		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
 425		  | LM90_HAVE_BROKEN_ALERT,
 426		.alert_alarms = 0x7c,
 427		.max_convrate = 9,
 428		.reg_local_ext = TMP451_REG_R_LOCAL_TEMPL,
 429	},
 430};
 431
 432/*
 433 * TEMP8 register index
 434 */
 435enum lm90_temp8_reg_index {
 436	LOCAL_LOW = 0,
 437	LOCAL_HIGH,
 438	LOCAL_CRIT,
 439	REMOTE_CRIT,
 440	LOCAL_EMERG,	/* max6659 and max6695/96 */
 441	REMOTE_EMERG,	/* max6659 and max6695/96 */
 442	REMOTE2_CRIT,	/* max6695/96 only */
 443	REMOTE2_EMERG,	/* max6695/96 only */
 444	TEMP8_REG_NUM
 445};
 446
 447/*
 448 * TEMP11 register index
 449 */
 450enum lm90_temp11_reg_index {
 451	REMOTE_TEMP = 0,
 452	REMOTE_LOW,
 453	REMOTE_HIGH,
 454	REMOTE_OFFSET,	/* except max6646, max6657/58/59, and max6695/96 */
 455	LOCAL_TEMP,
 456	REMOTE2_TEMP,	/* max6695/96 only */
 457	REMOTE2_LOW,	/* max6695/96 only */
 458	REMOTE2_HIGH,	/* max6695/96 only */
 459	TEMP11_REG_NUM
 460};
 461
 462/*
 463 * Client data (each client gets its own)
 464 */
 465
 466struct lm90_data {
 467	struct i2c_client *client;
 468	u32 channel_config[4];
 469	struct hwmon_channel_info temp_info;
 470	const struct hwmon_channel_info *info[3];
 471	struct hwmon_chip_info chip;
 472	struct mutex update_lock;
 473	bool valid;		/* true if register values are valid */
 
 474	unsigned long last_updated; /* in jiffies */
 475	int kind;
 476	u32 flags;
 477
 478	unsigned int update_interval; /* in milliseconds */
 479
 480	u8 config;		/* Current configuration register value */
 481	u8 config_orig;		/* Original configuration register value */
 482	u8 convrate_orig;	/* Original conversion rate register value */
 483	u16 alert_alarms;	/* Which alarm bits trigger ALERT# */
 484				/* Upper 8 bits for max6695/96 */
 485	u8 max_convrate;	/* Maximum conversion rate */
 486	u8 reg_local_ext;	/* local extension register offset */
 487
 488	/* registers values */
 489	s8 temp8[TEMP8_REG_NUM];
 490	s16 temp11[TEMP11_REG_NUM];
 491	u8 temp_hyst;
 492	u16 alarms; /* bitvector (upper 8 bits for max6695/96) */
 493};
 494
 495/*
 496 * Support functions
 497 */
 498
 499/*
 500 * The ADM1032 supports PEC but not on write byte transactions, so we need
 501 * to explicitly ask for a transaction without PEC.
 502 */
 503static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
 504{
 505	return i2c_smbus_xfer(client->adapter, client->addr,
 506			      client->flags & ~I2C_CLIENT_PEC,
 507			      I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
 508}
 509
 510/*
 511 * It is assumed that client->update_lock is held (unless we are in
 512 * detection or initialization steps). This matters when PEC is enabled,
 513 * because we don't want the address pointer to change between the write
 514 * byte and the read byte transactions.
 515 */
 516static int lm90_read_reg(struct i2c_client *client, u8 reg)
 517{
 518	int err;
 519
 520	if (client->flags & I2C_CLIENT_PEC) {
 521		err = adm1032_write_byte(client, reg);
 522		if (err >= 0)
 523			err = i2c_smbus_read_byte(client);
 524	} else
 525		err = i2c_smbus_read_byte_data(client, reg);
 526
 527	return err;
 
 
 
 
 
 
 
 528}
 529
 530static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl)
 531{
 532	int oldh, newh, l;
 
 533
 534	/*
 535	 * There is a trick here. We have to read two registers to have the
 536	 * sensor temperature, but we have to beware a conversion could occur
 537	 * between the readings. The datasheet says we should either use
 538	 * the one-shot conversion register, which we don't want to do
 539	 * (disables hardware monitoring) or monitor the busy bit, which is
 540	 * impossible (we can't read the values and monitor that bit at the
 541	 * exact same time). So the solution used here is to read the high
 542	 * byte once, then the low byte, then the high byte again. If the new
 543	 * high byte matches the old one, then we have a valid reading. Else
 544	 * we have to read the low byte again, and now we believe we have a
 545	 * correct reading.
 546	 */
 547	oldh = lm90_read_reg(client, regh);
 548	if (oldh < 0)
 549		return oldh;
 550	l = lm90_read_reg(client, regl);
 551	if (l < 0)
 552		return l;
 553	newh = lm90_read_reg(client, regh);
 554	if (newh < 0)
 555		return newh;
 556	if (oldh != newh) {
 557		l = lm90_read_reg(client, regl);
 558		if (l < 0)
 559			return l;
 560	}
 561	return (newh << 8) | l;
 562}
 563
 564static int lm90_update_confreg(struct lm90_data *data, u8 config)
 565{
 566	if (data->config != config) {
 567		int err;
 568
 569		err = i2c_smbus_write_byte_data(data->client,
 570						LM90_REG_W_CONFIG1,
 571						config);
 572		if (err)
 573			return err;
 574		data->config = config;
 575	}
 
 
 576	return 0;
 577}
 578
 579/*
 580 * client->update_lock must be held when calling this function (unless we are
 581 * in detection or initialization steps), and while a remote channel other
 582 * than channel 0 is selected. Also, calling code must make sure to re-select
 583 * external channel 0 before releasing the lock. This is necessary because
 584 * various registers have different meanings as a result of selecting a
 585 * non-default remote channel.
 586 */
 587static int lm90_select_remote_channel(struct lm90_data *data, int channel)
 
 
 588{
 589	int err = 0;
 590
 591	if (data->kind == max6696) {
 592		u8 config = data->config & ~0x08;
 593
 594		if (channel)
 595			config |= 0x08;
 596		err = lm90_update_confreg(data, config);
 597	}
 598	return err;
 599}
 600
 601static int lm90_write_convrate(struct lm90_data *data, int val)
 602{
 603	u8 config = data->config;
 604	int err;
 605
 606	/* Save config and pause conversion */
 607	if (data->flags & LM90_PAUSE_FOR_CONFIG) {
 608		err = lm90_update_confreg(data, config | 0x40);
 609		if (err < 0)
 610			return err;
 611	}
 612
 613	/* Set conv rate */
 614	err = i2c_smbus_write_byte_data(data->client, LM90_REG_W_CONVRATE, val);
 615
 616	/* Revert change to config */
 617	lm90_update_confreg(data, config);
 618
 619	return err;
 620}
 621
 622/*
 623 * Set conversion rate.
 624 * client->update_lock must be held when calling this function (unless we are
 625 * in detection or initialization steps).
 626 */
 627static int lm90_set_convrate(struct i2c_client *client, struct lm90_data *data,
 628			     unsigned int interval)
 629{
 
 630	unsigned int update_interval;
 631	int i, err;
 632
 633	/* Shift calculations to avoid rounding errors */
 634	interval <<= 6;
 635
 636	/* find the nearest update rate */
 637	for (i = 0, update_interval = LM90_MAX_CONVRATE_MS << 6;
 638	     i < data->max_convrate; i++, update_interval >>= 1)
 639		if (interval >= update_interval * 3 / 4)
 640			break;
 641
 642	err = lm90_write_convrate(data, i);
 643	data->update_interval = DIV_ROUND_CLOSEST(update_interval, 64);
 644	return err;
 645}
 646
 647static int lm90_update_limits(struct device *dev)
 648{
 649	struct lm90_data *data = dev_get_drvdata(dev);
 650	struct i2c_client *client = data->client;
 651	int val;
 652
 653	val = lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT);
 654	if (val < 0)
 655		return val;
 656	data->temp8[LOCAL_CRIT] = val;
 657
 658	val = lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT);
 659	if (val < 0)
 660		return val;
 661	data->temp8[REMOTE_CRIT] = val;
 662
 663	val = lm90_read_reg(client, LM90_REG_R_TCRIT_HYST);
 664	if (val < 0)
 665		return val;
 666	data->temp_hyst = val;
 667
 668	val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH);
 669	if (val < 0)
 670		return val;
 671	data->temp11[REMOTE_LOW] = val << 8;
 672
 673	if (data->flags & LM90_HAVE_REM_LIMIT_EXT) {
 674		val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL);
 675		if (val < 0)
 676			return val;
 677		data->temp11[REMOTE_LOW] |= val;
 678	}
 679
 680	val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH);
 681	if (val < 0)
 682		return val;
 683	data->temp11[REMOTE_HIGH] = val << 8;
 684
 685	if (data->flags & LM90_HAVE_REM_LIMIT_EXT) {
 686		val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL);
 687		if (val < 0)
 688			return val;
 689		data->temp11[REMOTE_HIGH] |= val;
 690	}
 691
 692	if (data->flags & LM90_HAVE_OFFSET) {
 693		val = lm90_read16(client, LM90_REG_R_REMOTE_OFFSH,
 694				  LM90_REG_R_REMOTE_OFFSL);
 695		if (val < 0)
 696			return val;
 697		data->temp11[REMOTE_OFFSET] = val;
 698	}
 699
 700	if (data->flags & LM90_HAVE_EMERGENCY) {
 701		val = lm90_read_reg(client, MAX6659_REG_R_LOCAL_EMERG);
 702		if (val < 0)
 703			return val;
 704		data->temp8[LOCAL_EMERG] = val;
 705
 706		val = lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG);
 707		if (val < 0)
 708			return val;
 709		data->temp8[REMOTE_EMERG] = val;
 710	}
 711
 712	if (data->kind == max6696) {
 713		val = lm90_select_remote_channel(data, 1);
 714		if (val < 0)
 715			return val;
 716
 717		val = lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT);
 718		if (val < 0)
 719			return val;
 720		data->temp8[REMOTE2_CRIT] = val;
 721
 722		val = lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG);
 723		if (val < 0)
 724			return val;
 725		data->temp8[REMOTE2_EMERG] = val;
 726
 727		val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH);
 728		if (val < 0)
 729			return val;
 730		data->temp11[REMOTE2_LOW] = val << 8;
 731
 732		val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH);
 733		if (val < 0)
 734			return val;
 735		data->temp11[REMOTE2_HIGH] = val << 8;
 736
 737		lm90_select_remote_channel(data, 0);
 738	}
 739
 740	return 0;
 741}
 742
 743static int lm90_update_device(struct device *dev)
 744{
 745	struct lm90_data *data = dev_get_drvdata(dev);
 746	struct i2c_client *client = data->client;
 747	unsigned long next_update;
 748	int val;
 749
 750	if (!data->valid) {
 751		val = lm90_update_limits(dev);
 752		if (val < 0)
 753			return val;
 754	}
 755
 756	next_update = data->last_updated +
 757		      msecs_to_jiffies(data->update_interval);
 758	if (time_after(jiffies, next_update) || !data->valid) {
 759		dev_dbg(&client->dev, "Updating lm90 data.\n");
 760
 761		data->valid = false;
 762
 763		val = lm90_read_reg(client, LM90_REG_R_LOCAL_LOW);
 764		if (val < 0)
 765			return val;
 766		data->temp8[LOCAL_LOW] = val;
 767
 768		val = lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH);
 769		if (val < 0)
 770			return val;
 771		data->temp8[LOCAL_HIGH] = val;
 
 772
 773		if (data->reg_local_ext) {
 774			val = lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
 775					  data->reg_local_ext);
 776			if (val < 0)
 777				return val;
 778			data->temp11[LOCAL_TEMP] = val;
 779		} else {
 780			val = lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP);
 781			if (val < 0)
 782				return val;
 783			data->temp11[LOCAL_TEMP] = val << 8;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 784		}
 785		val = lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
 786				  LM90_REG_R_REMOTE_TEMPL);
 787		if (val < 0)
 788			return val;
 789		data->temp11[REMOTE_TEMP] = val;
 790
 791		val = lm90_read_reg(client, LM90_REG_R_STATUS);
 792		if (val < 0)
 793			return val;
 794		data->alarms = val;	/* lower 8 bit of alarms */
 795
 796		if (data->kind == max6696) {
 797			val = lm90_select_remote_channel(data, 1);
 798			if (val < 0)
 799				return val;
 800
 801			val = lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
 802					  LM90_REG_R_REMOTE_TEMPL);
 803			if (val < 0) {
 804				lm90_select_remote_channel(data, 0);
 805				return val;
 806			}
 807			data->temp11[REMOTE2_TEMP] = val;
 808
 809			lm90_select_remote_channel(data, 0);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 810
 811			val = lm90_read_reg(client, MAX6696_REG_R_STATUS2);
 812			if (val < 0)
 813				return val;
 814			data->alarms |= val << 8;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 815		}
 816
 817		/*
 818		 * Re-enable ALERT# output if it was originally enabled and
 819		 * relevant alarms are all clear
 820		 */
 821		if (!(data->config_orig & 0x80) &&
 822		    !(data->alarms & data->alert_alarms)) {
 823			if (data->config & 0x80) {
 
 
 
 824				dev_dbg(&client->dev, "Re-enabling ALERT#\n");
 825				lm90_update_confreg(data, data->config & ~0x80);
 
 
 826			}
 827		}
 828
 829		data->last_updated = jiffies;
 830		data->valid = true;
 831	}
 832
 833	return 0;
 
 
 834}
 835
 836/*
 837 * Conversions
 838 * For local temperatures and limits, critical limits and the hysteresis
 839 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
 840 * For remote temperatures and limits, it uses signed 11-bit values with
 841 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers.  Some
 842 * Maxim chips use unsigned values.
 843 */
 844
 845static inline int temp_from_s8(s8 val)
 846{
 847	return val * 1000;
 848}
 849
 850static inline int temp_from_u8(u8 val)
 851{
 852	return val * 1000;
 853}
 854
 855static inline int temp_from_s16(s16 val)
 856{
 857	return val / 32 * 125;
 858}
 859
 860static inline int temp_from_u16(u16 val)
 861{
 862	return val / 32 * 125;
 863}
 864
 865static s8 temp_to_s8(long val)
 866{
 867	if (val <= -128000)
 868		return -128;
 869	if (val >= 127000)
 870		return 127;
 871	if (val < 0)
 872		return (val - 500) / 1000;
 873	return (val + 500) / 1000;
 874}
 875
 876static u8 temp_to_u8(long val)
 877{
 878	if (val <= 0)
 879		return 0;
 880	if (val >= 255000)
 881		return 255;
 882	return (val + 500) / 1000;
 883}
 884
 885static s16 temp_to_s16(long val)
 886{
 887	if (val <= -128000)
 888		return 0x8000;
 889	if (val >= 127875)
 890		return 0x7FE0;
 891	if (val < 0)
 892		return (val - 62) / 125 * 32;
 893	return (val + 62) / 125 * 32;
 894}
 895
 896static u8 hyst_to_reg(long val)
 897{
 898	if (val <= 0)
 899		return 0;
 900	if (val >= 30500)
 901		return 31;
 902	return (val + 500) / 1000;
 903}
 904
 905/*
 906 * ADT7461 in compatibility mode is almost identical to LM90 except that
 907 * attempts to write values that are outside the range 0 < temp < 127 are
 908 * treated as the boundary value.
 909 *
 910 * ADT7461 in "extended mode" operation uses unsigned integers offset by
 911 * 64 (e.g., 0 -> -64 degC).  The range is restricted to -64..191 degC.
 912 */
 913static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
 914{
 915	if (data->flags & LM90_FLAG_ADT7461_EXT)
 916		return (val - 64) * 1000;
 917	return temp_from_s8(val);
 
 918}
 919
 920static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
 921{
 922	if (data->flags & LM90_FLAG_ADT7461_EXT)
 923		return (val - 0x4000) / 64 * 250;
 924	return temp_from_s16(val);
 
 925}
 926
 927static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
 928{
 929	if (data->flags & LM90_FLAG_ADT7461_EXT) {
 930		if (val <= -64000)
 931			return 0;
 932		if (val >= 191000)
 933			return 0xFF;
 934		return (val + 500 + 64000) / 1000;
 
 
 
 
 
 
 935	}
 936	if (val <= 0)
 937		return 0;
 938	if (val >= 127000)
 939		return 127;
 940	return (val + 500) / 1000;
 941}
 942
 943static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
 944{
 945	if (data->flags & LM90_FLAG_ADT7461_EXT) {
 946		if (val <= -64000)
 947			return 0;
 948		if (val >= 191750)
 949			return 0xFFC0;
 950		return (val + 64000 + 125) / 250 * 64;
 
 
 
 
 
 
 951	}
 952	if (val <= 0)
 953		return 0;
 954	if (val >= 127750)
 955		return 0x7FC0;
 956	return (val + 125) / 250 * 64;
 957}
 958
 959/* pec used for ADM1032 only */
 960static ssize_t pec_show(struct device *dev, struct device_attribute *dummy,
 961			char *buf)
 
 
 
 962{
 963	struct i2c_client *client = to_i2c_client(dev);
 
 
 964
 965	return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
 
 
 
 
 
 
 
 
 
 
 
 966}
 967
 968static ssize_t pec_store(struct device *dev, struct device_attribute *dummy,
 969			 const char *buf, size_t count)
 970{
 971	struct i2c_client *client = to_i2c_client(dev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 972	long val;
 973	int err;
 974
 975	err = kstrtol(buf, 10, &val);
 976	if (err < 0)
 977		return err;
 978
 979	switch (val) {
 980	case 0:
 981		client->flags &= ~I2C_CLIENT_PEC;
 982		break;
 983	case 1:
 984		client->flags |= I2C_CLIENT_PEC;
 985		break;
 986	default:
 987		return -EINVAL;
 988	}
 
 989
 
 
 
 
 
 990	return count;
 991}
 992
 993static DEVICE_ATTR_RW(pec);
 994
 995static int lm90_get_temp11(struct lm90_data *data, int index)
 996{
 997	s16 temp11 = data->temp11[index];
 
 998	int temp;
 999
1000	if (data->kind == adt7461 || data->kind == tmp451)
1001		temp = temp_from_u16_adt7461(data, temp11);
1002	else if (data->kind == max6646)
1003		temp = temp_from_u16(temp11);
1004	else
1005		temp = temp_from_s16(temp11);
1006
1007	/* +16 degrees offset for temp2 for the LM99 */
1008	if (data->kind == lm99 && index <= 2)
1009		temp += 16000;
1010
1011	return temp;
1012}
1013
1014static int lm90_set_temp11(struct lm90_data *data, int index, long val)
 
1015{
1016	static struct reg {
1017		u8 high;
1018		u8 low;
1019	} reg[] = {
1020	[REMOTE_LOW] = { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL },
1021	[REMOTE_HIGH] = { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL },
1022	[REMOTE_OFFSET] = { LM90_REG_W_REMOTE_OFFSH, LM90_REG_W_REMOTE_OFFSL },
1023	[REMOTE2_LOW] = { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL },
1024	[REMOTE2_HIGH] = { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL }
 
1025	};
 
 
 
1026	struct i2c_client *client = data->client;
1027	struct reg *regp = &reg[index];
 
 
1028	int err;
1029
 
 
 
 
1030	/* +16 degrees offset for temp2 for the LM99 */
1031	if (data->kind == lm99 && index <= 2)
1032		val -= 16000;
1033
 
1034	if (data->kind == adt7461 || data->kind == tmp451)
1035		data->temp11[index] = temp_to_u16_adt7461(data, val);
1036	else if (data->kind == max6646)
1037		data->temp11[index] = temp_to_u8(val) << 8;
1038	else if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
1039		data->temp11[index] = temp_to_s16(val);
1040	else
1041		data->temp11[index] = temp_to_s8(val) << 8;
1042
1043	lm90_select_remote_channel(data, index >= 3);
1044	err = i2c_smbus_write_byte_data(client, regp->high,
1045				  data->temp11[index] >> 8);
1046	if (err < 0)
1047		return err;
1048	if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
1049		err = i2c_smbus_write_byte_data(client, regp->low,
1050						data->temp11[index] & 0xff);
 
1051
1052	lm90_select_remote_channel(data, 0);
1053	return err;
1054}
1055
1056static int lm90_get_temp8(struct lm90_data *data, int index)
 
 
1057{
1058	s8 temp8 = data->temp8[index];
 
1059	int temp;
1060
1061	if (data->kind == adt7461 || data->kind == tmp451)
1062		temp = temp_from_u8_adt7461(data, temp8);
1063	else if (data->kind == max6646)
1064		temp = temp_from_u8(temp8);
1065	else
1066		temp = temp_from_s8(temp8);
1067
1068	/* +16 degrees offset for temp2 for the LM99 */
1069	if (data->kind == lm99 && index == 3)
1070		temp += 16000;
1071
1072	return temp;
1073}
1074
1075static int lm90_set_temp8(struct lm90_data *data, int index, long val)
 
1076{
1077	static const u8 reg[TEMP8_REG_NUM] = {
1078		LM90_REG_W_LOCAL_LOW,
1079		LM90_REG_W_LOCAL_HIGH,
1080		LM90_REG_W_LOCAL_CRIT,
1081		LM90_REG_W_REMOTE_CRIT,
1082		MAX6659_REG_W_LOCAL_EMERG,
1083		MAX6659_REG_W_REMOTE_EMERG,
1084		LM90_REG_W_REMOTE_CRIT,
1085		MAX6659_REG_W_REMOTE_EMERG,
1086	};
1087	struct i2c_client *client = data->client;
 
1088	int err;
 
1089
1090	/* +16 degrees offset for temp2 for the LM99 */
1091	if (data->kind == lm99 && index == 3)
1092		val -= 16000;
1093
 
1094	if (data->kind == adt7461 || data->kind == tmp451)
1095		data->temp8[index] = temp_to_u8_adt7461(data, val);
1096	else if (data->kind == max6646)
1097		data->temp8[index] = temp_to_u8(val);
1098	else
1099		data->temp8[index] = temp_to_s8(val);
1100
1101	lm90_select_remote_channel(data, index >= 6);
1102	err = i2c_smbus_write_byte_data(client, reg[index], data->temp8[index]);
1103	lm90_select_remote_channel(data, 0);
 
 
 
1104
1105	return err;
 
 
 
 
1106}
1107
1108static int lm90_get_temphyst(struct lm90_data *data, int index)
 
1109{
1110	int temp;
 
 
1111
1112	if (data->kind == adt7461 || data->kind == tmp451)
1113		temp = temp_from_u8_adt7461(data, data->temp8[index]);
1114	else if (data->kind == max6646)
1115		temp = temp_from_u8(data->temp8[index]);
1116	else
1117		temp = temp_from_s8(data->temp8[index]);
1118
1119	/* +16 degrees offset for temp2 for the LM99 */
1120	if (data->kind == lm99 && index == 3)
1121		temp += 16000;
 
1122
1123	return temp - temp_from_s8(data->temp_hyst);
1124}
1125
1126static int lm90_set_temphyst(struct lm90_data *data, long val)
 
 
1127{
 
1128	struct i2c_client *client = data->client;
1129	int temp;
1130	int err;
1131
1132	if (data->kind == adt7461 || data->kind == tmp451)
1133		temp = temp_from_u8_adt7461(data, data->temp8[LOCAL_CRIT]);
1134	else if (data->kind == max6646)
1135		temp = temp_from_u8(data->temp8[LOCAL_CRIT]);
1136	else
1137		temp = temp_from_s8(data->temp8[LOCAL_CRIT]);
1138
1139	data->temp_hyst = hyst_to_reg(temp - val);
1140	err = i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
1141					data->temp_hyst);
1142	return err;
 
1143}
1144
1145static const u8 lm90_temp_index[3] = {
1146	LOCAL_TEMP, REMOTE_TEMP, REMOTE2_TEMP
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1147};
1148
1149static const u8 lm90_temp_min_index[3] = {
1150	LOCAL_LOW, REMOTE_LOW, REMOTE2_LOW
1151};
1152
1153static const u8 lm90_temp_max_index[3] = {
1154	LOCAL_HIGH, REMOTE_HIGH, REMOTE2_HIGH
 
1155};
1156
1157static const u8 lm90_temp_crit_index[3] = {
1158	LOCAL_CRIT, REMOTE_CRIT, REMOTE2_CRIT
1159};
1160
1161static const u8 lm90_temp_emerg_index[3] = {
1162	LOCAL_EMERG, REMOTE_EMERG, REMOTE2_EMERG
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1163};
1164
1165static const u8 lm90_min_alarm_bits[3] = { 5, 3, 11 };
1166static const u8 lm90_max_alarm_bits[3] = { 6, 4, 12 };
1167static const u8 lm90_crit_alarm_bits[3] = { 0, 1, 9 };
1168static const u8 lm90_emergency_alarm_bits[3] = { 15, 13, 14 };
1169static const u8 lm90_fault_bits[3] = { 0, 2, 10 };
1170
1171static int lm90_temp_read(struct device *dev, u32 attr, int channel, long *val)
1172{
1173	struct lm90_data *data = dev_get_drvdata(dev);
1174	int err;
1175
1176	mutex_lock(&data->update_lock);
1177	err = lm90_update_device(dev);
1178	mutex_unlock(&data->update_lock);
1179	if (err)
1180		return err;
1181
1182	switch (attr) {
1183	case hwmon_temp_input:
1184		*val = lm90_get_temp11(data, lm90_temp_index[channel]);
1185		break;
1186	case hwmon_temp_min_alarm:
1187		*val = (data->alarms >> lm90_min_alarm_bits[channel]) & 1;
1188		break;
1189	case hwmon_temp_max_alarm:
1190		*val = (data->alarms >> lm90_max_alarm_bits[channel]) & 1;
1191		break;
1192	case hwmon_temp_crit_alarm:
1193		*val = (data->alarms >> lm90_crit_alarm_bits[channel]) & 1;
1194		break;
1195	case hwmon_temp_emergency_alarm:
1196		*val = (data->alarms >> lm90_emergency_alarm_bits[channel]) & 1;
1197		break;
1198	case hwmon_temp_fault:
1199		*val = (data->alarms >> lm90_fault_bits[channel]) & 1;
1200		break;
1201	case hwmon_temp_min:
1202		if (channel == 0)
1203			*val = lm90_get_temp8(data,
1204					      lm90_temp_min_index[channel]);
1205		else
1206			*val = lm90_get_temp11(data,
1207					       lm90_temp_min_index[channel]);
1208		break;
1209	case hwmon_temp_max:
1210		if (channel == 0)
1211			*val = lm90_get_temp8(data,
1212					      lm90_temp_max_index[channel]);
1213		else
1214			*val = lm90_get_temp11(data,
1215					       lm90_temp_max_index[channel]);
1216		break;
1217	case hwmon_temp_crit:
1218		*val = lm90_get_temp8(data, lm90_temp_crit_index[channel]);
1219		break;
1220	case hwmon_temp_crit_hyst:
1221		*val = lm90_get_temphyst(data, lm90_temp_crit_index[channel]);
1222		break;
1223	case hwmon_temp_emergency:
1224		*val = lm90_get_temp8(data, lm90_temp_emerg_index[channel]);
1225		break;
1226	case hwmon_temp_emergency_hyst:
1227		*val = lm90_get_temphyst(data, lm90_temp_emerg_index[channel]);
1228		break;
1229	case hwmon_temp_offset:
1230		*val = lm90_get_temp11(data, REMOTE_OFFSET);
1231		break;
1232	default:
1233		return -EOPNOTSUPP;
1234	}
1235	return 0;
1236}
1237
1238static int lm90_temp_write(struct device *dev, u32 attr, int channel, long val)
1239{
1240	struct lm90_data *data = dev_get_drvdata(dev);
1241	int err;
1242
1243	mutex_lock(&data->update_lock);
 
 
 
 
1244
1245	err = lm90_update_device(dev);
1246	if (err)
1247		goto error;
1248
1249	switch (attr) {
1250	case hwmon_temp_min:
1251		if (channel == 0)
1252			err = lm90_set_temp8(data,
1253					      lm90_temp_min_index[channel],
1254					      val);
1255		else
1256			err = lm90_set_temp11(data,
1257					      lm90_temp_min_index[channel],
1258					      val);
1259		break;
1260	case hwmon_temp_max:
1261		if (channel == 0)
1262			err = lm90_set_temp8(data,
1263					     lm90_temp_max_index[channel],
1264					     val);
1265		else
1266			err = lm90_set_temp11(data,
1267					      lm90_temp_max_index[channel],
1268					      val);
1269		break;
1270	case hwmon_temp_crit:
1271		err = lm90_set_temp8(data, lm90_temp_crit_index[channel], val);
1272		break;
1273	case hwmon_temp_crit_hyst:
1274		err = lm90_set_temphyst(data, val);
1275		break;
1276	case hwmon_temp_emergency:
1277		err = lm90_set_temp8(data, lm90_temp_emerg_index[channel], val);
1278		break;
1279	case hwmon_temp_offset:
1280		err = lm90_set_temp11(data, REMOTE_OFFSET, val);
1281		break;
1282	default:
1283		err = -EOPNOTSUPP;
1284		break;
1285	}
1286error:
1287	mutex_unlock(&data->update_lock);
 
1288
1289	return err;
1290}
 
1291
1292static umode_t lm90_temp_is_visible(const void *data, u32 attr, int channel)
 
 
1293{
1294	switch (attr) {
1295	case hwmon_temp_input:
1296	case hwmon_temp_min_alarm:
1297	case hwmon_temp_max_alarm:
1298	case hwmon_temp_crit_alarm:
1299	case hwmon_temp_emergency_alarm:
1300	case hwmon_temp_emergency_hyst:
1301	case hwmon_temp_fault:
1302		return 0444;
1303	case hwmon_temp_min:
1304	case hwmon_temp_max:
1305	case hwmon_temp_crit:
1306	case hwmon_temp_emergency:
1307	case hwmon_temp_offset:
1308		return 0644;
1309	case hwmon_temp_crit_hyst:
1310		if (channel == 0)
1311			return 0644;
1312		return 0444;
1313	default:
1314		return 0;
1315	}
1316}
1317
1318static int lm90_chip_read(struct device *dev, u32 attr, int channel, long *val)
 
1319{
1320	struct lm90_data *data = dev_get_drvdata(dev);
 
1321	int err;
1322
1323	mutex_lock(&data->update_lock);
1324	err = lm90_update_device(dev);
1325	mutex_unlock(&data->update_lock);
1326	if (err)
1327		return err;
1328
1329	switch (attr) {
1330	case hwmon_chip_update_interval:
1331		*val = data->update_interval;
1332		break;
1333	case hwmon_chip_alarms:
1334		*val = data->alarms;
1335		break;
1336	default:
1337		return -EOPNOTSUPP;
1338	}
1339
1340	return 0;
1341}
1342
1343static int lm90_chip_write(struct device *dev, u32 attr, int channel, long val)
1344{
1345	struct lm90_data *data = dev_get_drvdata(dev);
1346	struct i2c_client *client = data->client;
1347	int err;
1348
1349	mutex_lock(&data->update_lock);
1350
1351	err = lm90_update_device(dev);
1352	if (err)
1353		goto error;
1354
1355	switch (attr) {
1356	case hwmon_chip_update_interval:
1357		err = lm90_set_convrate(client, data,
1358					clamp_val(val, 0, 100000));
1359		break;
1360	default:
1361		err = -EOPNOTSUPP;
1362		break;
1363	}
1364error:
1365	mutex_unlock(&data->update_lock);
1366
1367	return err;
1368}
1369
1370static umode_t lm90_chip_is_visible(const void *data, u32 attr, int channel)
1371{
1372	switch (attr) {
1373	case hwmon_chip_update_interval:
1374		return 0644;
1375	case hwmon_chip_alarms:
1376		return 0444;
1377	default:
1378		return 0;
1379	}
1380}
1381
1382static int lm90_read(struct device *dev, enum hwmon_sensor_types type,
1383		     u32 attr, int channel, long *val)
1384{
1385	switch (type) {
1386	case hwmon_chip:
1387		return lm90_chip_read(dev, attr, channel, val);
1388	case hwmon_temp:
1389		return lm90_temp_read(dev, attr, channel, val);
1390	default:
1391		return -EOPNOTSUPP;
1392	}
1393}
1394
1395static int lm90_write(struct device *dev, enum hwmon_sensor_types type,
1396		      u32 attr, int channel, long val)
1397{
1398	switch (type) {
1399	case hwmon_chip:
1400		return lm90_chip_write(dev, attr, channel, val);
1401	case hwmon_temp:
1402		return lm90_temp_write(dev, attr, channel, val);
1403	default:
1404		return -EOPNOTSUPP;
1405	}
1406}
1407
1408static umode_t lm90_is_visible(const void *data, enum hwmon_sensor_types type,
1409			       u32 attr, int channel)
1410{
1411	switch (type) {
1412	case hwmon_chip:
1413		return lm90_chip_is_visible(data, attr, channel);
1414	case hwmon_temp:
1415		return lm90_temp_is_visible(data, attr, channel);
1416	default:
1417		return 0;
1418	}
1419}
1420
1421/* Return 0 if detection is successful, -ENODEV otherwise */
1422static int lm90_detect(struct i2c_client *client,
1423		       struct i2c_board_info *info)
1424{
1425	struct i2c_adapter *adapter = client->adapter;
1426	int address = client->addr;
1427	const char *name = NULL;
1428	int man_id, chip_id, config1, config2, convrate;
1429
1430	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1431		return -ENODEV;
1432
1433	/* detection and identification */
1434	man_id = i2c_smbus_read_byte_data(client, LM90_REG_R_MAN_ID);
1435	chip_id = i2c_smbus_read_byte_data(client, LM90_REG_R_CHIP_ID);
1436	config1 = i2c_smbus_read_byte_data(client, LM90_REG_R_CONFIG1);
1437	convrate = i2c_smbus_read_byte_data(client, LM90_REG_R_CONVRATE);
1438	if (man_id < 0 || chip_id < 0 || config1 < 0 || convrate < 0)
1439		return -ENODEV;
1440
1441	if (man_id == 0x01 || man_id == 0x5C || man_id == 0x41) {
1442		config2 = i2c_smbus_read_byte_data(client, LM90_REG_R_CONFIG2);
1443		if (config2 < 0)
1444			return -ENODEV;
1445	} else
1446		config2 = 0;		/* Make compiler happy */
1447
1448	if ((address == 0x4C || address == 0x4D)
1449	 && man_id == 0x01) { /* National Semiconductor */
1450		if ((config1 & 0x2A) == 0x00
1451		 && (config2 & 0xF8) == 0x00
1452		 && convrate <= 0x09) {
1453			if (address == 0x4C
1454			 && (chip_id & 0xF0) == 0x20) { /* LM90 */
1455				name = "lm90";
1456			} else
1457			if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
1458				name = "lm99";
1459				dev_info(&adapter->dev,
1460					 "Assuming LM99 chip at 0x%02x\n",
1461					 address);
1462				dev_info(&adapter->dev,
1463					 "If it is an LM89, instantiate it "
1464					 "with the new_device sysfs "
1465					 "interface\n");
1466			} else
1467			if (address == 0x4C
1468			 && (chip_id & 0xF0) == 0x10) { /* LM86 */
1469				name = "lm86";
1470			}
1471		}
1472	} else
1473	if ((address == 0x4C || address == 0x4D)
1474	 && man_id == 0x41) { /* Analog Devices */
1475		if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
1476		 && (config1 & 0x3F) == 0x00
1477		 && convrate <= 0x0A) {
1478			name = "adm1032";
1479			/*
1480			 * The ADM1032 supports PEC, but only if combined
1481			 * transactions are not used.
1482			 */
1483			if (i2c_check_functionality(adapter,
1484						    I2C_FUNC_SMBUS_BYTE))
1485				info->flags |= I2C_CLIENT_PEC;
1486		} else
1487		if (chip_id == 0x51 /* ADT7461 */
1488		 && (config1 & 0x1B) == 0x00
1489		 && convrate <= 0x0A) {
1490			name = "adt7461";
1491		} else
1492		if (chip_id == 0x57 /* ADT7461A, NCT1008 */
1493		 && (config1 & 0x1B) == 0x00
1494		 && convrate <= 0x0A) {
1495			name = "adt7461a";
1496		}
1497	} else
1498	if (man_id == 0x4D) { /* Maxim */
1499		int emerg, emerg2, status2;
1500
1501		/*
1502		 * We read MAX6659_REG_R_REMOTE_EMERG twice, and re-read
1503		 * LM90_REG_R_MAN_ID in between. If MAX6659_REG_R_REMOTE_EMERG
1504		 * exists, both readings will reflect the same value. Otherwise,
1505		 * the readings will be different.
1506		 */
1507		emerg = i2c_smbus_read_byte_data(client,
1508						 MAX6659_REG_R_REMOTE_EMERG);
1509		man_id = i2c_smbus_read_byte_data(client,
1510						  LM90_REG_R_MAN_ID);
1511		emerg2 = i2c_smbus_read_byte_data(client,
1512						  MAX6659_REG_R_REMOTE_EMERG);
1513		status2 = i2c_smbus_read_byte_data(client,
1514						   MAX6696_REG_R_STATUS2);
1515		if (emerg < 0 || man_id < 0 || emerg2 < 0 || status2 < 0)
1516			return -ENODEV;
1517
1518		/*
1519		 * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
1520		 * register. Reading from that address will return the last
1521		 * read value, which in our case is those of the man_id
1522		 * register. Likewise, the config1 register seems to lack a
1523		 * low nibble, so the value will be those of the previous
1524		 * read, so in our case those of the man_id register.
1525		 * MAX6659 has a third set of upper temperature limit registers.
1526		 * Those registers also return values on MAX6657 and MAX6658,
1527		 * thus the only way to detect MAX6659 is by its address.
1528		 * For this reason it will be mis-detected as MAX6657 if its
1529		 * address is 0x4C.
1530		 */
1531		if (chip_id == man_id
1532		 && (address == 0x4C || address == 0x4D || address == 0x4E)
1533		 && (config1 & 0x1F) == (man_id & 0x0F)
1534		 && convrate <= 0x09) {
1535			if (address == 0x4C)
1536				name = "max6657";
1537			else
1538				name = "max6659";
1539		} else
1540		/*
1541		 * Even though MAX6695 and MAX6696 do not have a chip ID
1542		 * register, reading it returns 0x01. Bit 4 of the config1
1543		 * register is unused and should return zero when read. Bit 0 of
1544		 * the status2 register is unused and should return zero when
1545		 * read.
1546		 *
1547		 * MAX6695 and MAX6696 have an additional set of temperature
1548		 * limit registers. We can detect those chips by checking if
1549		 * one of those registers exists.
1550		 */
1551		if (chip_id == 0x01
1552		 && (config1 & 0x10) == 0x00
1553		 && (status2 & 0x01) == 0x00
1554		 && emerg == emerg2
1555		 && convrate <= 0x07) {
1556			name = "max6696";
1557		} else
1558		/*
1559		 * The chip_id register of the MAX6680 and MAX6681 holds the
1560		 * revision of the chip. The lowest bit of the config1 register
1561		 * is unused and should return zero when read, so should the
1562		 * second to last bit of config1 (software reset).
1563		 */
1564		if (chip_id == 0x01
1565		 && (config1 & 0x03) == 0x00
1566		 && convrate <= 0x07) {
1567			name = "max6680";
1568		} else
1569		/*
1570		 * The chip_id register of the MAX6646/6647/6649 holds the
1571		 * revision of the chip. The lowest 6 bits of the config1
1572		 * register are unused and should return zero when read.
1573		 */
1574		if (chip_id == 0x59
1575		 && (config1 & 0x3f) == 0x00
1576		 && convrate <= 0x07) {
1577			name = "max6646";
1578		} else
1579		/*
1580		 * The chip_id of the MAX6654 holds the revision of the chip.
1581		 * The lowest 3 bits of the config1 register are unused and
1582		 * should return zero when read.
1583		 */
1584		if (chip_id == 0x08
1585		 && (config1 & 0x07) == 0x00
1586		 && convrate <= 0x07) {
1587			name = "max6654";
1588		}
1589	} else
1590	if (address == 0x4C
1591	 && man_id == 0x5C) { /* Winbond/Nuvoton */
1592		if ((config1 & 0x2A) == 0x00
1593		 && (config2 & 0xF8) == 0x00) {
1594			if (chip_id == 0x01 /* W83L771W/G */
1595			 && convrate <= 0x09) {
1596				name = "w83l771";
1597			} else
1598			if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */
1599			 && convrate <= 0x08) {
1600				name = "w83l771";
1601			}
1602		}
1603	} else
1604	if (address >= 0x48 && address <= 0x4F
1605	 && man_id == 0xA1) { /*  NXP Semiconductor/Philips */
1606		if (chip_id == 0x00
1607		 && (config1 & 0x2A) == 0x00
1608		 && (config2 & 0xFE) == 0x00
1609		 && convrate <= 0x09) {
1610			name = "sa56004";
1611		}
1612	} else
1613	if ((address == 0x4C || address == 0x4D)
1614	 && man_id == 0x47) { /* GMT */
1615		if (chip_id == 0x01 /* G781 */
1616		 && (config1 & 0x3F) == 0x00
1617		 && convrate <= 0x08)
1618			name = "g781";
1619	} else
1620	if (address == 0x4C
1621	 && man_id == 0x55) { /* Texas Instruments */
1622		int local_ext;
1623
1624		local_ext = i2c_smbus_read_byte_data(client,
1625						     TMP451_REG_R_LOCAL_TEMPL);
1626
1627		if (chip_id == 0x00 /* TMP451 */
1628		 && (config1 & 0x1B) == 0x00
1629		 && convrate <= 0x09
1630		 && (local_ext & 0x0F) == 0x00)
1631			name = "tmp451";
1632	}
1633
1634	if (!name) { /* identification failed */
1635		dev_dbg(&adapter->dev,
1636			"Unsupported chip at 0x%02x (man_id=0x%02X, "
1637			"chip_id=0x%02X)\n", address, man_id, chip_id);
1638		return -ENODEV;
1639	}
1640
1641	strlcpy(info->type, name, I2C_NAME_SIZE);
1642
1643	return 0;
1644}
1645
1646static void lm90_restore_conf(void *_data)
1647{
1648	struct lm90_data *data = _data;
1649	struct i2c_client *client = data->client;
1650
1651	/* Restore initial configuration */
1652	lm90_write_convrate(data, data->convrate_orig);
 
1653	i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
1654				  data->config_orig);
1655}
1656
1657static int lm90_init_client(struct i2c_client *client, struct lm90_data *data)
1658{
1659	int config, convrate;
1660
1661	convrate = lm90_read_reg(client, LM90_REG_R_CONVRATE);
1662	if (convrate < 0)
1663		return convrate;
 
1664	data->convrate_orig = convrate;
1665
1666	/*
1667	 * Start the conversions.
1668	 */
1669	config = lm90_read_reg(client, LM90_REG_R_CONFIG1);
1670	if (config < 0)
1671		return config;
 
 
1672	data->config_orig = config;
1673	data->config = config;
1674
1675	lm90_set_convrate(client, data, 500); /* 500ms; 2Hz conversion rate */
1676
1677	/* Check Temperature Range Select */
1678	if (data->kind == adt7461 || data->kind == tmp451) {
1679		if (config & 0x04)
1680			data->flags |= LM90_FLAG_ADT7461_EXT;
1681	}
1682
1683	/*
1684	 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
1685	 * 0.125 degree resolution) and range (0x08, extend range
1686	 * to -64 degree) mode for the remote temperature sensor.
1687	 */
1688	if (data->kind == max6680)
1689		config |= 0x18;
1690
1691	/*
1692	 * Put MAX6654 into extended range (0x20, extend minimum range from
1693	 * 0 degrees to -64 degrees). Note that extended resolution is not
1694	 * possible on the MAX6654 unless conversion rate is set to 1 Hz or
1695	 * slower, which is intentionally not done by default.
1696	 */
1697	if (data->kind == max6654)
1698		config |= 0x20;
1699
1700	/*
1701	 * Select external channel 0 for max6695/96
1702	 */
1703	if (data->kind == max6696)
1704		config &= ~0x08;
1705
1706	config &= 0xBF;	/* run */
1707	lm90_update_confreg(data, config);
1708
1709	return devm_add_action_or_reset(&client->dev, lm90_restore_conf, data);
1710}
1711
1712static bool lm90_is_tripped(struct i2c_client *client, u16 *status)
1713{
1714	struct lm90_data *data = i2c_get_clientdata(client);
1715	int st, st2 = 0;
1716
1717	st = lm90_read_reg(client, LM90_REG_R_STATUS);
1718	if (st < 0)
1719		return false;
1720
1721	if (data->kind == max6696) {
1722		st2 = lm90_read_reg(client, MAX6696_REG_R_STATUS2);
1723		if (st2 < 0)
1724			return false;
1725	}
1726
1727	*status = st | (st2 << 8);
1728
1729	if ((st & 0x7f) == 0 && (st2 & 0xfe) == 0)
1730		return false;
1731
1732	if ((st & (LM90_STATUS_LLOW | LM90_STATUS_LHIGH | LM90_STATUS_LTHRM)) ||
1733	    (st2 & MAX6696_STATUS2_LOT2))
1734		dev_warn(&client->dev,
1735			 "temp%d out of range, please check!\n", 1);
1736	if ((st & (LM90_STATUS_RLOW | LM90_STATUS_RHIGH | LM90_STATUS_RTHRM)) ||
1737	    (st2 & MAX6696_STATUS2_ROT2))
1738		dev_warn(&client->dev,
1739			 "temp%d out of range, please check!\n", 2);
1740	if (st & LM90_STATUS_ROPEN)
1741		dev_warn(&client->dev,
1742			 "temp%d diode open, please check!\n", 2);
1743	if (st2 & (MAX6696_STATUS2_R2LOW | MAX6696_STATUS2_R2HIGH |
1744		   MAX6696_STATUS2_R2THRM | MAX6696_STATUS2_R2OT2))
1745		dev_warn(&client->dev,
1746			 "temp%d out of range, please check!\n", 3);
1747	if (st2 & MAX6696_STATUS2_R2OPEN)
1748		dev_warn(&client->dev,
1749			 "temp%d diode open, please check!\n", 3);
1750
1751	return true;
1752}
1753
1754static irqreturn_t lm90_irq_thread(int irq, void *dev_id)
1755{
1756	struct i2c_client *client = dev_id;
1757	u16 status;
1758
1759	if (lm90_is_tripped(client, &status))
1760		return IRQ_HANDLED;
1761	else
1762		return IRQ_NONE;
1763}
1764
1765static void lm90_remove_pec(void *dev)
1766{
1767	device_remove_file(dev, &dev_attr_pec);
1768}
1769
1770static void lm90_regulator_disable(void *regulator)
1771{
1772	regulator_disable(regulator);
1773}
1774
1775
1776static const struct hwmon_ops lm90_ops = {
1777	.is_visible = lm90_is_visible,
1778	.read = lm90_read,
1779	.write = lm90_write,
1780};
1781
1782static int lm90_probe(struct i2c_client *client,
1783		      const struct i2c_device_id *id)
1784{
1785	struct device *dev = &client->dev;
1786	struct i2c_adapter *adapter = client->adapter;
1787	struct hwmon_channel_info *info;
1788	struct regulator *regulator;
1789	struct device *hwmon_dev;
1790	struct lm90_data *data;
 
 
1791	int err;
1792
1793	regulator = devm_regulator_get(dev, "vcc");
1794	if (IS_ERR(regulator))
1795		return PTR_ERR(regulator);
1796
1797	err = regulator_enable(regulator);
1798	if (err < 0) {
1799		dev_err(dev, "Failed to enable regulator: %d\n", err);
1800		return err;
1801	}
1802
1803	err = devm_add_action_or_reset(dev, lm90_regulator_disable, regulator);
1804	if (err)
1805		return err;
1806
1807	data = devm_kzalloc(dev, sizeof(struct lm90_data), GFP_KERNEL);
1808	if (!data)
1809		return -ENOMEM;
1810
1811	data->client = client;
1812	i2c_set_clientdata(client, data);
1813	mutex_init(&data->update_lock);
1814
 
 
1815	/* Set the device type */
1816	if (client->dev.of_node)
1817		data->kind = (enum chips)of_device_get_match_data(&client->dev);
1818	else
1819		data->kind = id->driver_data;
1820	if (data->kind == adm1032) {
1821		if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
1822			client->flags &= ~I2C_CLIENT_PEC;
1823	}
1824
1825	/*
1826	 * Different devices have different alarm bits triggering the
1827	 * ALERT# output
1828	 */
1829	data->alert_alarms = lm90_params[data->kind].alert_alarms;
1830
1831	/* Set chip capabilities */
1832	data->flags = lm90_params[data->kind].flags;
 
1833
1834	data->chip.ops = &lm90_ops;
1835	data->chip.info = data->info;
1836
1837	data->info[0] = HWMON_CHANNEL_INFO(chip,
1838		HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL | HWMON_C_ALARMS);
1839	data->info[1] = &data->temp_info;
1840
1841	info = &data->temp_info;
1842	info->type = hwmon_temp;
1843	info->config = data->channel_config;
1844
1845	data->channel_config[0] = HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
1846		HWMON_T_CRIT | HWMON_T_CRIT_HYST | HWMON_T_MIN_ALARM |
1847		HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM;
1848	data->channel_config[1] = HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
1849		HWMON_T_CRIT | HWMON_T_CRIT_HYST | HWMON_T_MIN_ALARM |
1850		HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | HWMON_T_FAULT;
1851
1852	if (data->flags & LM90_HAVE_OFFSET)
1853		data->channel_config[1] |= HWMON_T_OFFSET;
1854
1855	if (data->flags & LM90_HAVE_EMERGENCY) {
1856		data->channel_config[0] |= HWMON_T_EMERGENCY |
1857			HWMON_T_EMERGENCY_HYST;
1858		data->channel_config[1] |= HWMON_T_EMERGENCY |
1859			HWMON_T_EMERGENCY_HYST;
1860	}
1861
1862	if (data->flags & LM90_HAVE_EMERGENCY_ALARM) {
1863		data->channel_config[0] |= HWMON_T_EMERGENCY_ALARM;
1864		data->channel_config[1] |= HWMON_T_EMERGENCY_ALARM;
1865	}
1866
1867	if (data->flags & LM90_HAVE_TEMP3) {
1868		data->channel_config[2] = HWMON_T_INPUT |
1869			HWMON_T_MIN | HWMON_T_MAX |
1870			HWMON_T_CRIT | HWMON_T_CRIT_HYST |
1871			HWMON_T_EMERGENCY | HWMON_T_EMERGENCY_HYST |
1872			HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM |
1873			HWMON_T_CRIT_ALARM | HWMON_T_EMERGENCY_ALARM |
1874			HWMON_T_FAULT;
1875	}
1876
1877	data->reg_local_ext = lm90_params[data->kind].reg_local_ext;
 
1878
1879	/* Set maximum conversion rate */
1880	data->max_convrate = lm90_params[data->kind].max_convrate;
1881
1882	/* Initialize the LM90 chip */
1883	err = lm90_init_client(client, data);
1884	if (err < 0) {
1885		dev_err(dev, "Failed to initialize device\n");
1886		return err;
1887	}
1888
1889	/*
1890	 * The 'pec' attribute is attached to the i2c device and thus created
1891	 * separately.
1892	 */
1893	if (client->flags & I2C_CLIENT_PEC) {
1894		err = device_create_file(dev, &dev_attr_pec);
1895		if (err)
1896			return err;
1897		err = devm_add_action_or_reset(dev, lm90_remove_pec, dev);
1898		if (err)
1899			return err;
1900	}
1901
1902	hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
1903							 data, &data->chip,
1904							 NULL);
1905	if (IS_ERR(hwmon_dev))
1906		return PTR_ERR(hwmon_dev);
 
1907
1908	if (client->irq) {
1909		dev_dbg(dev, "IRQ: %d\n", client->irq);
1910		err = devm_request_threaded_irq(dev, client->irq,
1911						NULL, lm90_irq_thread,
1912						IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1913						"lm90", client);
1914		if (err < 0) {
1915			dev_err(dev, "cannot request IRQ %d\n", client->irq);
1916			return err;
1917		}
1918	}
1919
1920	return 0;
 
 
 
 
 
 
 
 
 
 
1921}
1922
1923static void lm90_alert(struct i2c_client *client, enum i2c_alert_protocol type,
1924		       unsigned int flag)
1925{
1926	u16 alarms;
1927
1928	if (type != I2C_PROTOCOL_SMBUS_ALERT)
1929		return;
 
 
 
 
 
 
 
 
 
1930
1931	if (lm90_is_tripped(client, &alarms)) {
1932		/*
1933		 * Disable ALERT# output, because these chips don't implement
1934		 * SMBus alert correctly; they should only hold the alert line
1935		 * low briefly.
1936		 */
1937		struct lm90_data *data = i2c_get_clientdata(client);
1938
1939		if ((data->flags & LM90_HAVE_BROKEN_ALERT) &&
1940		    (alarms & data->alert_alarms)) {
 
1941			dev_dbg(&client->dev, "Disabling ALERT#\n");
1942			lm90_update_confreg(data, data->config | 0x80);
 
 
1943		}
1944	} else {
1945		dev_info(&client->dev, "Everything OK\n");
1946	}
1947}
1948
1949static struct i2c_driver lm90_driver = {
1950	.class		= I2C_CLASS_HWMON,
1951	.driver = {
1952		.name	= "lm90",
1953		.of_match_table = of_match_ptr(lm90_of_match),
1954	},
1955	.probe		= lm90_probe,
 
1956	.alert		= lm90_alert,
1957	.id_table	= lm90_id,
1958	.detect		= lm90_detect,
1959	.address_list	= normal_i2c,
1960};
1961
1962module_i2c_driver(lm90_driver);
1963
1964MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
1965MODULE_DESCRIPTION("LM90/ADM1032 driver");
1966MODULE_LICENSE("GPL");
v4.6
 
   1/*
   2 * lm90.c - Part of lm_sensors, Linux kernel modules for hardware
   3 *          monitoring
   4 * Copyright (C) 2003-2010  Jean Delvare <jdelvare@suse.de>
   5 *
   6 * Based on the lm83 driver. The LM90 is a sensor chip made by National
   7 * Semiconductor. It reports up to two temperatures (its own plus up to
   8 * one external one) with a 0.125 deg resolution (1 deg for local
   9 * temperature) and a 3-4 deg accuracy.
  10 *
  11 * This driver also supports the LM89 and LM99, two other sensor chips
  12 * made by National Semiconductor. Both have an increased remote
  13 * temperature measurement accuracy (1 degree), and the LM99
  14 * additionally shifts remote temperatures (measured and limits) by 16
  15 * degrees, which allows for higher temperatures measurement.
  16 * Note that there is no way to differentiate between both chips.
  17 * When device is auto-detected, the driver will assume an LM99.
  18 *
  19 * This driver also supports the LM86, another sensor chip made by
  20 * National Semiconductor. It is exactly similar to the LM90 except it
  21 * has a higher accuracy.
  22 *
  23 * This driver also supports the ADM1032, a sensor chip made by Analog
  24 * Devices. That chip is similar to the LM90, with a few differences
  25 * that are not handled by this driver. Among others, it has a higher
  26 * accuracy than the LM90, much like the LM86 does.
  27 *
  28 * This driver also supports the MAX6657, MAX6658 and MAX6659 sensor
  29 * chips made by Maxim. These chips are similar to the LM86.
  30 * Note that there is no easy way to differentiate between the three
  31 * variants. We use the device address to detect MAX6659, which will result
  32 * in a detection as max6657 if it is on address 0x4c. The extra address
  33 * and features of the MAX6659 are only supported if the chip is configured
  34 * explicitly as max6659, or if its address is not 0x4c.
  35 * These chips lack the remote temperature offset feature.
  36 *
 
 
 
 
 
 
 
 
  37 * This driver also supports the MAX6646, MAX6647, MAX6648, MAX6649 and
  38 * MAX6692 chips made by Maxim.  These are again similar to the LM86,
  39 * but they use unsigned temperature values and can report temperatures
  40 * from 0 to 145 degrees.
  41 *
  42 * This driver also supports the MAX6680 and MAX6681, two other sensor
  43 * chips made by Maxim. These are quite similar to the other Maxim
  44 * chips. The MAX6680 and MAX6681 only differ in the pinout so they can
  45 * be treated identically.
  46 *
  47 * This driver also supports the MAX6695 and MAX6696, two other sensor
  48 * chips made by Maxim. These are also quite similar to other Maxim
  49 * chips, but support three temperature sensors instead of two. MAX6695
  50 * and MAX6696 only differ in the pinout so they can be treated identically.
  51 *
  52 * This driver also supports ADT7461 and ADT7461A from Analog Devices as well as
  53 * NCT1008 from ON Semiconductor. The chips are supported in both compatibility
  54 * and extended mode. They are mostly compatible with LM90 except for a data
  55 * format difference for the temperature value registers.
  56 *
  57 * This driver also supports the SA56004 from Philips. This device is
  58 * pin-compatible with the LM86, the ED/EDP parts are also address-compatible.
  59 *
  60 * This driver also supports the G781 from GMT. This device is compatible
  61 * with the ADM1032.
  62 *
  63 * This driver also supports TMP451 from Texas Instruments. This device is
  64 * supported in both compatibility and extended mode. It's mostly compatible
  65 * with ADT7461 except for local temperature low byte register and max
  66 * conversion rate.
  67 *
  68 * Since the LM90 was the first chipset supported by this driver, most
  69 * comments will refer to this chipset, but are actually general and
  70 * concern all supported chipsets, unless mentioned otherwise.
  71 *
  72 * This program is free software; you can redistribute it and/or modify
  73 * it under the terms of the GNU General Public License as published by
  74 * the Free Software Foundation; either version 2 of the License, or
  75 * (at your option) any later version.
  76 *
  77 * This program is distributed in the hope that it will be useful,
  78 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  79 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  80 * GNU General Public License for more details.
  81 *
  82 * You should have received a copy of the GNU General Public License
  83 * along with this program; if not, write to the Free Software
  84 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  85 */
  86
  87#include <linux/module.h>
  88#include <linux/init.h>
  89#include <linux/slab.h>
  90#include <linux/jiffies.h>
  91#include <linux/i2c.h>
  92#include <linux/hwmon-sysfs.h>
  93#include <linux/hwmon.h>
  94#include <linux/err.h>
  95#include <linux/mutex.h>
 
  96#include <linux/sysfs.h>
  97#include <linux/interrupt.h>
  98#include <linux/regulator/consumer.h>
  99
 100/*
 101 * Addresses to scan
 102 * Address is fully defined internally and cannot be changed except for
 103 * MAX6659, MAX6680 and MAX6681.
 104 * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, ADT7461A, MAX6649,
 105 * MAX6657, MAX6658, NCT1008 and W83L771 have address 0x4c.
 106 * ADM1032-2, ADT7461-2, ADT7461A-2, LM89-1, LM99-1, MAX6646, and NCT1008D
 107 * have address 0x4d.
 108 * MAX6647 has address 0x4e.
 109 * MAX6659 can have address 0x4c, 0x4d or 0x4e.
 110 * MAX6680 and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b,
 111 * 0x4c, 0x4d or 0x4e.
 112 * SA56004 can have address 0x48 through 0x4F.
 113 */
 114
 115static const unsigned short normal_i2c[] = {
 116	0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
 117	0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
 118
 119enum chips { lm90, adm1032, lm99, lm86, max6657, max6659, adt7461, max6680,
 120	max6646, w83l771, max6696, sa56004, g781, tmp451 };
 121
 122/*
 123 * The LM90 registers
 124 */
 125
 126#define LM90_REG_R_MAN_ID		0xFE
 127#define LM90_REG_R_CHIP_ID		0xFF
 128#define LM90_REG_R_CONFIG1		0x03
 129#define LM90_REG_W_CONFIG1		0x09
 130#define LM90_REG_R_CONFIG2		0xBF
 131#define LM90_REG_W_CONFIG2		0xBF
 132#define LM90_REG_R_CONVRATE		0x04
 133#define LM90_REG_W_CONVRATE		0x0A
 134#define LM90_REG_R_STATUS		0x02
 135#define LM90_REG_R_LOCAL_TEMP		0x00
 136#define LM90_REG_R_LOCAL_HIGH		0x05
 137#define LM90_REG_W_LOCAL_HIGH		0x0B
 138#define LM90_REG_R_LOCAL_LOW		0x06
 139#define LM90_REG_W_LOCAL_LOW		0x0C
 140#define LM90_REG_R_LOCAL_CRIT		0x20
 141#define LM90_REG_W_LOCAL_CRIT		0x20
 142#define LM90_REG_R_REMOTE_TEMPH		0x01
 143#define LM90_REG_R_REMOTE_TEMPL		0x10
 144#define LM90_REG_R_REMOTE_OFFSH		0x11
 145#define LM90_REG_W_REMOTE_OFFSH		0x11
 146#define LM90_REG_R_REMOTE_OFFSL		0x12
 147#define LM90_REG_W_REMOTE_OFFSL		0x12
 148#define LM90_REG_R_REMOTE_HIGHH		0x07
 149#define LM90_REG_W_REMOTE_HIGHH		0x0D
 150#define LM90_REG_R_REMOTE_HIGHL		0x13
 151#define LM90_REG_W_REMOTE_HIGHL		0x13
 152#define LM90_REG_R_REMOTE_LOWH		0x08
 153#define LM90_REG_W_REMOTE_LOWH		0x0E
 154#define LM90_REG_R_REMOTE_LOWL		0x14
 155#define LM90_REG_W_REMOTE_LOWL		0x14
 156#define LM90_REG_R_REMOTE_CRIT		0x19
 157#define LM90_REG_W_REMOTE_CRIT		0x19
 158#define LM90_REG_R_TCRIT_HYST		0x21
 159#define LM90_REG_W_TCRIT_HYST		0x21
 160
 161/* MAX6646/6647/6649/6657/6658/6659/6695/6696 registers */
 162
 163#define MAX6657_REG_R_LOCAL_TEMPL	0x11
 164#define MAX6696_REG_R_STATUS2		0x12
 165#define MAX6659_REG_R_REMOTE_EMERG	0x16
 166#define MAX6659_REG_W_REMOTE_EMERG	0x16
 167#define MAX6659_REG_R_LOCAL_EMERG	0x17
 168#define MAX6659_REG_W_LOCAL_EMERG	0x17
 169
 170/*  SA56004 registers */
 171
 172#define SA56004_REG_R_LOCAL_TEMPL 0x22
 173
 174#define LM90_DEF_CONVRATE_RVAL	6	/* Def conversion rate register value */
 175#define LM90_MAX_CONVRATE_MS	16000	/* Maximum conversion rate in ms */
 176
 177/* TMP451 registers */
 178#define TMP451_REG_R_LOCAL_TEMPL	0x15
 179
 180/*
 181 * Device flags
 182 */
 183#define LM90_FLAG_ADT7461_EXT	(1 << 0) /* ADT7461 extended mode	*/
 184/* Device features */
 185#define LM90_HAVE_OFFSET	(1 << 1) /* temperature offset register	*/
 186#define LM90_HAVE_REM_LIMIT_EXT	(1 << 3) /* extended remote limit	*/
 187#define LM90_HAVE_EMERGENCY	(1 << 4) /* 3rd upper (emergency) limit	*/
 188#define LM90_HAVE_EMERGENCY_ALARM (1 << 5)/* emergency alarm		*/
 189#define LM90_HAVE_TEMP3		(1 << 6) /* 3rd temperature sensor	*/
 190#define LM90_HAVE_BROKEN_ALERT	(1 << 7) /* Broken alert		*/
 
 191
 192/* LM90 status */
 193#define LM90_STATUS_LTHRM	(1 << 0) /* local THERM limit tripped */
 194#define LM90_STATUS_RTHRM	(1 << 1) /* remote THERM limit tripped */
 195#define LM90_STATUS_ROPEN	(1 << 2) /* remote is an open circuit */
 196#define LM90_STATUS_RLOW	(1 << 3) /* remote low temp limit tripped */
 197#define LM90_STATUS_RHIGH	(1 << 4) /* remote high temp limit tripped */
 198#define LM90_STATUS_LLOW	(1 << 5) /* local low temp limit tripped */
 199#define LM90_STATUS_LHIGH	(1 << 6) /* local high temp limit tripped */
 200
 201#define MAX6696_STATUS2_R2THRM	(1 << 1) /* remote2 THERM limit tripped */
 202#define MAX6696_STATUS2_R2OPEN	(1 << 2) /* remote2 is an open circuit */
 203#define MAX6696_STATUS2_R2LOW	(1 << 3) /* remote2 low temp limit tripped */
 204#define MAX6696_STATUS2_R2HIGH	(1 << 4) /* remote2 high temp limit tripped */
 205#define MAX6696_STATUS2_ROT2	(1 << 5) /* remote emergency limit tripped */
 206#define MAX6696_STATUS2_R2OT2	(1 << 6) /* remote2 emergency limit tripped */
 207#define MAX6696_STATUS2_LOT2	(1 << 7) /* local emergency limit tripped */
 208
 209/*
 210 * Driver data (common to all clients)
 211 */
 212
 213static const struct i2c_device_id lm90_id[] = {
 214	{ "adm1032", adm1032 },
 215	{ "adt7461", adt7461 },
 216	{ "adt7461a", adt7461 },
 217	{ "g781", g781 },
 218	{ "lm90", lm90 },
 219	{ "lm86", lm86 },
 220	{ "lm89", lm86 },
 221	{ "lm99", lm99 },
 222	{ "max6646", max6646 },
 223	{ "max6647", max6646 },
 224	{ "max6649", max6646 },
 
 225	{ "max6657", max6657 },
 226	{ "max6658", max6657 },
 227	{ "max6659", max6659 },
 228	{ "max6680", max6680 },
 229	{ "max6681", max6680 },
 230	{ "max6695", max6696 },
 231	{ "max6696", max6696 },
 232	{ "nct1008", adt7461 },
 233	{ "w83l771", w83l771 },
 234	{ "sa56004", sa56004 },
 235	{ "tmp451", tmp451 },
 236	{ }
 237};
 238MODULE_DEVICE_TABLE(i2c, lm90_id);
 239
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 240/*
 241 * chip type specific parameters
 242 */
 243struct lm90_params {
 244	u32 flags;		/* Capabilities */
 245	u16 alert_alarms;	/* Which alarm bits trigger ALERT# */
 246				/* Upper 8 bits for max6695/96 */
 247	u8 max_convrate;	/* Maximum conversion rate register value */
 248	u8 reg_local_ext;	/* Extended local temp register (optional) */
 249};
 250
 251static const struct lm90_params lm90_params[] = {
 252	[adm1032] = {
 253		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
 254		  | LM90_HAVE_BROKEN_ALERT,
 255		.alert_alarms = 0x7c,
 256		.max_convrate = 10,
 257	},
 258	[adt7461] = {
 259		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
 260		  | LM90_HAVE_BROKEN_ALERT,
 261		.alert_alarms = 0x7c,
 262		.max_convrate = 10,
 263	},
 264	[g781] = {
 265		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
 266		  | LM90_HAVE_BROKEN_ALERT,
 267		.alert_alarms = 0x7c,
 268		.max_convrate = 8,
 269	},
 270	[lm86] = {
 271		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
 272		.alert_alarms = 0x7b,
 273		.max_convrate = 9,
 274	},
 275	[lm90] = {
 276		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
 277		.alert_alarms = 0x7b,
 278		.max_convrate = 9,
 279	},
 280	[lm99] = {
 281		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
 282		.alert_alarms = 0x7b,
 283		.max_convrate = 9,
 284	},
 285	[max6646] = {
 286		.alert_alarms = 0x7c,
 287		.max_convrate = 6,
 288		.reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
 289	},
 
 
 
 
 
 290	[max6657] = {
 
 291		.alert_alarms = 0x7c,
 292		.max_convrate = 8,
 293		.reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
 294	},
 295	[max6659] = {
 296		.flags = LM90_HAVE_EMERGENCY,
 297		.alert_alarms = 0x7c,
 298		.max_convrate = 8,
 299		.reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
 300	},
 301	[max6680] = {
 302		.flags = LM90_HAVE_OFFSET,
 303		.alert_alarms = 0x7c,
 304		.max_convrate = 7,
 305	},
 306	[max6696] = {
 307		.flags = LM90_HAVE_EMERGENCY
 308		  | LM90_HAVE_EMERGENCY_ALARM | LM90_HAVE_TEMP3,
 309		.alert_alarms = 0x1c7c,
 310		.max_convrate = 6,
 311		.reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
 312	},
 313	[w83l771] = {
 314		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
 315		.alert_alarms = 0x7c,
 316		.max_convrate = 8,
 317	},
 318	[sa56004] = {
 319		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
 320		.alert_alarms = 0x7b,
 321		.max_convrate = 9,
 322		.reg_local_ext = SA56004_REG_R_LOCAL_TEMPL,
 323	},
 324	[tmp451] = {
 325		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
 326		  | LM90_HAVE_BROKEN_ALERT,
 327		.alert_alarms = 0x7c,
 328		.max_convrate = 9,
 329		.reg_local_ext = TMP451_REG_R_LOCAL_TEMPL,
 330	}
 331};
 332
 333/*
 334 * TEMP8 register index
 335 */
 336enum lm90_temp8_reg_index {
 337	LOCAL_LOW = 0,
 338	LOCAL_HIGH,
 339	LOCAL_CRIT,
 340	REMOTE_CRIT,
 341	LOCAL_EMERG,	/* max6659 and max6695/96 */
 342	REMOTE_EMERG,	/* max6659 and max6695/96 */
 343	REMOTE2_CRIT,	/* max6695/96 only */
 344	REMOTE2_EMERG,	/* max6695/96 only */
 345	TEMP8_REG_NUM
 346};
 347
 348/*
 349 * TEMP11 register index
 350 */
 351enum lm90_temp11_reg_index {
 352	REMOTE_TEMP = 0,
 353	REMOTE_LOW,
 354	REMOTE_HIGH,
 355	REMOTE_OFFSET,	/* except max6646, max6657/58/59, and max6695/96 */
 356	LOCAL_TEMP,
 357	REMOTE2_TEMP,	/* max6695/96 only */
 358	REMOTE2_LOW,	/* max6695/96 only */
 359	REMOTE2_HIGH,	/* max6695/96 only */
 360	TEMP11_REG_NUM
 361};
 362
 363/*
 364 * Client data (each client gets its own)
 365 */
 366
 367struct lm90_data {
 368	struct i2c_client *client;
 369	struct device *hwmon_dev;
 370	const struct attribute_group *groups[6];
 
 
 371	struct mutex update_lock;
 372	struct regulator *regulator;
 373	char valid; /* zero until following fields are valid */
 374	unsigned long last_updated; /* in jiffies */
 375	int kind;
 376	u32 flags;
 377
 378	int update_interval;	/* in milliseconds */
 379
 
 380	u8 config_orig;		/* Original configuration register value */
 381	u8 convrate_orig;	/* Original conversion rate register value */
 382	u16 alert_alarms;	/* Which alarm bits trigger ALERT# */
 383				/* Upper 8 bits for max6695/96 */
 384	u8 max_convrate;	/* Maximum conversion rate */
 385	u8 reg_local_ext;	/* local extension register offset */
 386
 387	/* registers values */
 388	s8 temp8[TEMP8_REG_NUM];
 389	s16 temp11[TEMP11_REG_NUM];
 390	u8 temp_hyst;
 391	u16 alarms; /* bitvector (upper 8 bits for max6695/96) */
 392};
 393
 394/*
 395 * Support functions
 396 */
 397
 398/*
 399 * The ADM1032 supports PEC but not on write byte transactions, so we need
 400 * to explicitly ask for a transaction without PEC.
 401 */
 402static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
 403{
 404	return i2c_smbus_xfer(client->adapter, client->addr,
 405			      client->flags & ~I2C_CLIENT_PEC,
 406			      I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
 407}
 408
 409/*
 410 * It is assumed that client->update_lock is held (unless we are in
 411 * detection or initialization steps). This matters when PEC is enabled,
 412 * because we don't want the address pointer to change between the write
 413 * byte and the read byte transactions.
 414 */
 415static int lm90_read_reg(struct i2c_client *client, u8 reg, u8 *value)
 416{
 417	int err;
 418
 419	if (client->flags & I2C_CLIENT_PEC) {
 420		err = adm1032_write_byte(client, reg);
 421		if (err >= 0)
 422			err = i2c_smbus_read_byte(client);
 423	} else
 424		err = i2c_smbus_read_byte_data(client, reg);
 425
 426	if (err < 0) {
 427		dev_warn(&client->dev, "Register %#02x read failed (%d)\n",
 428			 reg, err);
 429		return err;
 430	}
 431	*value = err;
 432
 433	return 0;
 434}
 435
 436static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value)
 437{
 438	int err;
 439	u8 oldh, newh, l;
 440
 441	/*
 442	 * There is a trick here. We have to read two registers to have the
 443	 * sensor temperature, but we have to beware a conversion could occur
 444	 * between the readings. The datasheet says we should either use
 445	 * the one-shot conversion register, which we don't want to do
 446	 * (disables hardware monitoring) or monitor the busy bit, which is
 447	 * impossible (we can't read the values and monitor that bit at the
 448	 * exact same time). So the solution used here is to read the high
 449	 * byte once, then the low byte, then the high byte again. If the new
 450	 * high byte matches the old one, then we have a valid reading. Else
 451	 * we have to read the low byte again, and now we believe we have a
 452	 * correct reading.
 453	 */
 454	if ((err = lm90_read_reg(client, regh, &oldh))
 455	 || (err = lm90_read_reg(client, regl, &l))
 456	 || (err = lm90_read_reg(client, regh, &newh)))
 457		return err;
 
 
 
 
 
 458	if (oldh != newh) {
 459		err = lm90_read_reg(client, regl, &l);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 460		if (err)
 461			return err;
 
 462	}
 463	*value = (newh << 8) | l;
 464
 465	return 0;
 466}
 467
 468/*
 469 * client->update_lock must be held when calling this function (unless we are
 470 * in detection or initialization steps), and while a remote channel other
 471 * than channel 0 is selected. Also, calling code must make sure to re-select
 472 * external channel 0 before releasing the lock. This is necessary because
 473 * various registers have different meanings as a result of selecting a
 474 * non-default remote channel.
 475 */
 476static inline void lm90_select_remote_channel(struct i2c_client *client,
 477					      struct lm90_data *data,
 478					      int channel)
 479{
 480	u8 config;
 481
 482	if (data->kind == max6696) {
 483		lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
 484		config &= ~0x08;
 485		if (channel)
 486			config |= 0x08;
 487		i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
 488					  config);
 
 
 
 
 
 
 
 
 
 
 
 
 
 489	}
 
 
 
 
 
 
 
 
 490}
 491
 492/*
 493 * Set conversion rate.
 494 * client->update_lock must be held when calling this function (unless we are
 495 * in detection or initialization steps).
 496 */
 497static void lm90_set_convrate(struct i2c_client *client, struct lm90_data *data,
 498			      unsigned int interval)
 499{
 500	int i;
 501	unsigned int update_interval;
 
 502
 503	/* Shift calculations to avoid rounding errors */
 504	interval <<= 6;
 505
 506	/* find the nearest update rate */
 507	for (i = 0, update_interval = LM90_MAX_CONVRATE_MS << 6;
 508	     i < data->max_convrate; i++, update_interval >>= 1)
 509		if (interval >= update_interval * 3 / 4)
 510			break;
 511
 512	i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE, i);
 513	data->update_interval = DIV_ROUND_CLOSEST(update_interval, 64);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 514}
 515
 516static struct lm90_data *lm90_update_device(struct device *dev)
 517{
 518	struct lm90_data *data = dev_get_drvdata(dev);
 519	struct i2c_client *client = data->client;
 520	unsigned long next_update;
 
 521
 522	mutex_lock(&data->update_lock);
 
 
 
 
 523
 524	next_update = data->last_updated +
 525		      msecs_to_jiffies(data->update_interval);
 526	if (time_after(jiffies, next_update) || !data->valid) {
 527		u8 h, l;
 528		u8 alarms;
 
 529
 530		dev_dbg(&client->dev, "Updating lm90 data.\n");
 531		lm90_read_reg(client, LM90_REG_R_LOCAL_LOW,
 532			      &data->temp8[LOCAL_LOW]);
 533		lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH,
 534			      &data->temp8[LOCAL_HIGH]);
 535		lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT,
 536			      &data->temp8[LOCAL_CRIT]);
 537		lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT,
 538			      &data->temp8[REMOTE_CRIT]);
 539		lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst);
 540
 541		if (data->reg_local_ext) {
 542			lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
 543				    data->reg_local_ext,
 544				    &data->temp11[LOCAL_TEMP]);
 
 
 545		} else {
 546			if (lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP,
 547					  &h) == 0)
 548				data->temp11[LOCAL_TEMP] = h << 8;
 549		}
 550		lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
 551			    LM90_REG_R_REMOTE_TEMPL,
 552			    &data->temp11[REMOTE_TEMP]);
 553
 554		if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h) == 0) {
 555			data->temp11[REMOTE_LOW] = h << 8;
 556			if ((data->flags & LM90_HAVE_REM_LIMIT_EXT)
 557			 && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL,
 558					  &l) == 0)
 559				data->temp11[REMOTE_LOW] |= l;
 560		}
 561		if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h) == 0) {
 562			data->temp11[REMOTE_HIGH] = h << 8;
 563			if ((data->flags & LM90_HAVE_REM_LIMIT_EXT)
 564			 && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL,
 565					  &l) == 0)
 566				data->temp11[REMOTE_HIGH] |= l;
 567		}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 568
 569		if (data->flags & LM90_HAVE_OFFSET) {
 570			if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH,
 571					  &h) == 0
 572			 && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL,
 573					  &l) == 0)
 574				data->temp11[REMOTE_OFFSET] = (h << 8) | l;
 575		}
 576		if (data->flags & LM90_HAVE_EMERGENCY) {
 577			lm90_read_reg(client, MAX6659_REG_R_LOCAL_EMERG,
 578				      &data->temp8[LOCAL_EMERG]);
 579			lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG,
 580				      &data->temp8[REMOTE_EMERG]);
 581		}
 582		lm90_read_reg(client, LM90_REG_R_STATUS, &alarms);
 583		data->alarms = alarms;	/* save as 16 bit value */
 584
 585		if (data->kind == max6696) {
 586			lm90_select_remote_channel(client, data, 1);
 587			lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT,
 588				      &data->temp8[REMOTE2_CRIT]);
 589			lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG,
 590				      &data->temp8[REMOTE2_EMERG]);
 591			lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
 592				    LM90_REG_R_REMOTE_TEMPL,
 593				    &data->temp11[REMOTE2_TEMP]);
 594			if (!lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h))
 595				data->temp11[REMOTE2_LOW] = h << 8;
 596			if (!lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h))
 597				data->temp11[REMOTE2_HIGH] = h << 8;
 598			lm90_select_remote_channel(client, data, 0);
 599
 600			if (!lm90_read_reg(client, MAX6696_REG_R_STATUS2,
 601					   &alarms))
 602				data->alarms |= alarms << 8;
 603		}
 604
 605		/*
 606		 * Re-enable ALERT# output if it was originally enabled and
 607		 * relevant alarms are all clear
 608		 */
 609		if ((data->config_orig & 0x80) == 0
 610		 && (data->alarms & data->alert_alarms) == 0) {
 611			u8 config;
 612
 613			lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
 614			if (config & 0x80) {
 615				dev_dbg(&client->dev, "Re-enabling ALERT#\n");
 616				i2c_smbus_write_byte_data(client,
 617							  LM90_REG_W_CONFIG1,
 618							  config & ~0x80);
 619			}
 620		}
 621
 622		data->last_updated = jiffies;
 623		data->valid = 1;
 624	}
 625
 626	mutex_unlock(&data->update_lock);
 627
 628	return data;
 629}
 630
 631/*
 632 * Conversions
 633 * For local temperatures and limits, critical limits and the hysteresis
 634 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
 635 * For remote temperatures and limits, it uses signed 11-bit values with
 636 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers.  Some
 637 * Maxim chips use unsigned values.
 638 */
 639
 640static inline int temp_from_s8(s8 val)
 641{
 642	return val * 1000;
 643}
 644
 645static inline int temp_from_u8(u8 val)
 646{
 647	return val * 1000;
 648}
 649
 650static inline int temp_from_s16(s16 val)
 651{
 652	return val / 32 * 125;
 653}
 654
 655static inline int temp_from_u16(u16 val)
 656{
 657	return val / 32 * 125;
 658}
 659
 660static s8 temp_to_s8(long val)
 661{
 662	if (val <= -128000)
 663		return -128;
 664	if (val >= 127000)
 665		return 127;
 666	if (val < 0)
 667		return (val - 500) / 1000;
 668	return (val + 500) / 1000;
 669}
 670
 671static u8 temp_to_u8(long val)
 672{
 673	if (val <= 0)
 674		return 0;
 675	if (val >= 255000)
 676		return 255;
 677	return (val + 500) / 1000;
 678}
 679
 680static s16 temp_to_s16(long val)
 681{
 682	if (val <= -128000)
 683		return 0x8000;
 684	if (val >= 127875)
 685		return 0x7FE0;
 686	if (val < 0)
 687		return (val - 62) / 125 * 32;
 688	return (val + 62) / 125 * 32;
 689}
 690
 691static u8 hyst_to_reg(long val)
 692{
 693	if (val <= 0)
 694		return 0;
 695	if (val >= 30500)
 696		return 31;
 697	return (val + 500) / 1000;
 698}
 699
 700/*
 701 * ADT7461 in compatibility mode is almost identical to LM90 except that
 702 * attempts to write values that are outside the range 0 < temp < 127 are
 703 * treated as the boundary value.
 704 *
 705 * ADT7461 in "extended mode" operation uses unsigned integers offset by
 706 * 64 (e.g., 0 -> -64 degC).  The range is restricted to -64..191 degC.
 707 */
 708static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
 709{
 710	if (data->flags & LM90_FLAG_ADT7461_EXT)
 711		return (val - 64) * 1000;
 712	else
 713		return temp_from_s8(val);
 714}
 715
 716static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
 717{
 718	if (data->flags & LM90_FLAG_ADT7461_EXT)
 719		return (val - 0x4000) / 64 * 250;
 720	else
 721		return temp_from_s16(val);
 722}
 723
 724static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
 725{
 726	if (data->flags & LM90_FLAG_ADT7461_EXT) {
 727		if (val <= -64000)
 728			return 0;
 729		if (val >= 191000)
 730			return 0xFF;
 731		return (val + 500 + 64000) / 1000;
 732	} else {
 733		if (val <= 0)
 734			return 0;
 735		if (val >= 127000)
 736			return 127;
 737		return (val + 500) / 1000;
 738	}
 
 
 
 
 
 739}
 740
 741static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
 742{
 743	if (data->flags & LM90_FLAG_ADT7461_EXT) {
 744		if (val <= -64000)
 745			return 0;
 746		if (val >= 191750)
 747			return 0xFFC0;
 748		return (val + 64000 + 125) / 250 * 64;
 749	} else {
 750		if (val <= 0)
 751			return 0;
 752		if (val >= 127750)
 753			return 0x7FC0;
 754		return (val + 125) / 250 * 64;
 755	}
 
 
 
 
 
 756}
 757
 758/*
 759 * Sysfs stuff
 760 */
 761
 762static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
 763			  char *buf)
 764{
 765	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 766	struct lm90_data *data = lm90_update_device(dev);
 767	int temp;
 768
 769	if (data->kind == adt7461 || data->kind == tmp451)
 770		temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
 771	else if (data->kind == max6646)
 772		temp = temp_from_u8(data->temp8[attr->index]);
 773	else
 774		temp = temp_from_s8(data->temp8[attr->index]);
 775
 776	/* +16 degrees offset for temp2 for the LM99 */
 777	if (data->kind == lm99 && attr->index == 3)
 778		temp += 16000;
 779
 780	return sprintf(buf, "%d\n", temp);
 781}
 782
 783static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
 784			 const char *buf, size_t count)
 785{
 786	static const u8 reg[TEMP8_REG_NUM] = {
 787		LM90_REG_W_LOCAL_LOW,
 788		LM90_REG_W_LOCAL_HIGH,
 789		LM90_REG_W_LOCAL_CRIT,
 790		LM90_REG_W_REMOTE_CRIT,
 791		MAX6659_REG_W_LOCAL_EMERG,
 792		MAX6659_REG_W_REMOTE_EMERG,
 793		LM90_REG_W_REMOTE_CRIT,
 794		MAX6659_REG_W_REMOTE_EMERG,
 795	};
 796
 797	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 798	struct lm90_data *data = dev_get_drvdata(dev);
 799	struct i2c_client *client = data->client;
 800	int nr = attr->index;
 801	long val;
 802	int err;
 803
 804	err = kstrtol(buf, 10, &val);
 805	if (err < 0)
 806		return err;
 807
 808	/* +16 degrees offset for temp2 for the LM99 */
 809	if (data->kind == lm99 && attr->index == 3)
 810		val -= 16000;
 811
 812	mutex_lock(&data->update_lock);
 813	if (data->kind == adt7461 || data->kind == tmp451)
 814		data->temp8[nr] = temp_to_u8_adt7461(data, val);
 815	else if (data->kind == max6646)
 816		data->temp8[nr] = temp_to_u8(val);
 817	else
 818		data->temp8[nr] = temp_to_s8(val);
 819
 820	lm90_select_remote_channel(client, data, nr >= 6);
 821	i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]);
 822	lm90_select_remote_channel(client, data, 0);
 823
 824	mutex_unlock(&data->update_lock);
 825	return count;
 826}
 827
 828static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
 829			   char *buf)
 
 830{
 831	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 832	struct lm90_data *data = lm90_update_device(dev);
 833	int temp;
 834
 835	if (data->kind == adt7461 || data->kind == tmp451)
 836		temp = temp_from_u16_adt7461(data, data->temp11[attr->index]);
 837	else if (data->kind == max6646)
 838		temp = temp_from_u16(data->temp11[attr->index]);
 839	else
 840		temp = temp_from_s16(data->temp11[attr->index]);
 841
 842	/* +16 degrees offset for temp2 for the LM99 */
 843	if (data->kind == lm99 &&  attr->index <= 2)
 844		temp += 16000;
 845
 846	return sprintf(buf, "%d\n", temp);
 847}
 848
 849static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
 850			  const char *buf, size_t count)
 851{
 852	struct {
 853		u8 high;
 854		u8 low;
 855		int channel;
 856	} reg[5] = {
 857		{ LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL, 0 },
 858		{ LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL, 0 },
 859		{ LM90_REG_W_REMOTE_OFFSH, LM90_REG_W_REMOTE_OFFSL, 0 },
 860		{ LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL, 1 },
 861		{ LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL, 1 }
 862	};
 863
 864	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 865	struct lm90_data *data = dev_get_drvdata(dev);
 866	struct i2c_client *client = data->client;
 867	int nr = attr->nr;
 868	int index = attr->index;
 869	long val;
 870	int err;
 871
 872	err = kstrtol(buf, 10, &val);
 873	if (err < 0)
 874		return err;
 875
 876	/* +16 degrees offset for temp2 for the LM99 */
 877	if (data->kind == lm99 && index <= 2)
 878		val -= 16000;
 879
 880	mutex_lock(&data->update_lock);
 881	if (data->kind == adt7461 || data->kind == tmp451)
 882		data->temp11[index] = temp_to_u16_adt7461(data, val);
 883	else if (data->kind == max6646)
 884		data->temp11[index] = temp_to_u8(val) << 8;
 885	else if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
 886		data->temp11[index] = temp_to_s16(val);
 887	else
 888		data->temp11[index] = temp_to_s8(val) << 8;
 889
 890	lm90_select_remote_channel(client, data, reg[nr].channel);
 891	i2c_smbus_write_byte_data(client, reg[nr].high,
 892				  data->temp11[index] >> 8);
 
 
 893	if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
 894		i2c_smbus_write_byte_data(client, reg[nr].low,
 895					  data->temp11[index] & 0xff);
 896	lm90_select_remote_channel(client, data, 0);
 897
 898	mutex_unlock(&data->update_lock);
 899	return count;
 900}
 901
 902static ssize_t show_temphyst(struct device *dev,
 903			     struct device_attribute *devattr,
 904			     char *buf)
 905{
 906	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 907	struct lm90_data *data = lm90_update_device(dev);
 908	int temp;
 909
 910	if (data->kind == adt7461 || data->kind == tmp451)
 911		temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
 912	else if (data->kind == max6646)
 913		temp = temp_from_u8(data->temp8[attr->index]);
 914	else
 915		temp = temp_from_s8(data->temp8[attr->index]);
 916
 917	/* +16 degrees offset for temp2 for the LM99 */
 918	if (data->kind == lm99 && attr->index == 3)
 919		temp += 16000;
 920
 921	return sprintf(buf, "%d\n", temp - temp_from_s8(data->temp_hyst));
 922}
 923
 924static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy,
 925			    const char *buf, size_t count)
 926{
 927	struct lm90_data *data = dev_get_drvdata(dev);
 
 
 
 
 
 
 
 
 
 928	struct i2c_client *client = data->client;
 929	long val;
 930	int err;
 931	int temp;
 932
 933	err = kstrtol(buf, 10, &val);
 934	if (err < 0)
 935		return err;
 936
 937	mutex_lock(&data->update_lock);
 938	if (data->kind == adt7461 || data->kind == tmp451)
 939		temp = temp_from_u8_adt7461(data, data->temp8[LOCAL_CRIT]);
 940	else if (data->kind == max6646)
 941		temp = temp_from_u8(data->temp8[LOCAL_CRIT]);
 942	else
 943		temp = temp_from_s8(data->temp8[LOCAL_CRIT]);
 944
 945	data->temp_hyst = hyst_to_reg(temp - val);
 946	i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
 947				  data->temp_hyst);
 948	mutex_unlock(&data->update_lock);
 949	return count;
 950}
 951
 952static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
 953			   char *buf)
 954{
 955	struct lm90_data *data = lm90_update_device(dev);
 956	return sprintf(buf, "%d\n", data->alarms);
 957}
 958
 959static ssize_t show_alarm(struct device *dev, struct device_attribute
 960			  *devattr, char *buf)
 961{
 962	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 963	struct lm90_data *data = lm90_update_device(dev);
 964	int bitnr = attr->index;
 965
 966	return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
 967}
 
 
 
 
 968
 969static ssize_t show_update_interval(struct device *dev,
 970				    struct device_attribute *attr, char *buf)
 971{
 972	struct lm90_data *data = dev_get_drvdata(dev);
 973
 974	return sprintf(buf, "%u\n", data->update_interval);
 975}
 976
 977static ssize_t set_update_interval(struct device *dev,
 978				   struct device_attribute *attr,
 979				   const char *buf, size_t count)
 980{
 981	struct lm90_data *data = dev_get_drvdata(dev);
 982	struct i2c_client *client = data->client;
 983	unsigned long val;
 984	int err;
 985
 986	err = kstrtoul(buf, 10, &val);
 987	if (err)
 988		return err;
 
 
 
 989
 990	mutex_lock(&data->update_lock);
 991	lm90_set_convrate(client, data, clamp_val(val, 0, 100000));
 992	mutex_unlock(&data->update_lock);
 993
 994	return count;
 995}
 996
 997static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp11, NULL,
 998	0, LOCAL_TEMP);
 999static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp11, NULL,
1000	0, REMOTE_TEMP);
1001static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8,
1002	set_temp8, LOCAL_LOW);
1003static SENSOR_DEVICE_ATTR_2(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
1004	set_temp11, 0, REMOTE_LOW);
1005static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
1006	set_temp8, LOCAL_HIGH);
1007static SENSOR_DEVICE_ATTR_2(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
1008	set_temp11, 1, REMOTE_HIGH);
1009static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8,
1010	set_temp8, LOCAL_CRIT);
1011static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8,
1012	set_temp8, REMOTE_CRIT);
1013static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst,
1014	set_temphyst, LOCAL_CRIT);
1015static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL,
1016	REMOTE_CRIT);
1017static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
1018	set_temp11, 2, REMOTE_OFFSET);
1019
1020/* Individual alarm files */
1021static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
1022static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
1023static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
1024static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
1025static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
1026static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
1027static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
1028/* Raw alarm file for compatibility */
1029static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
1030
1031static DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, show_update_interval,
1032		   set_update_interval);
1033
1034static struct attribute *lm90_attributes[] = {
1035	&sensor_dev_attr_temp1_input.dev_attr.attr,
1036	&sensor_dev_attr_temp2_input.dev_attr.attr,
1037	&sensor_dev_attr_temp1_min.dev_attr.attr,
1038	&sensor_dev_attr_temp2_min.dev_attr.attr,
1039	&sensor_dev_attr_temp1_max.dev_attr.attr,
1040	&sensor_dev_attr_temp2_max.dev_attr.attr,
1041	&sensor_dev_attr_temp1_crit.dev_attr.attr,
1042	&sensor_dev_attr_temp2_crit.dev_attr.attr,
1043	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
1044	&sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
1045
1046	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
1047	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
1048	&sensor_dev_attr_temp2_fault.dev_attr.attr,
1049	&sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
1050	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
1051	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
1052	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
1053	&dev_attr_alarms.attr,
1054	&dev_attr_update_interval.attr,
1055	NULL
1056};
1057
1058static const struct attribute_group lm90_group = {
1059	.attrs = lm90_attributes,
1060};
1061
1062static struct attribute *lm90_temp2_offset_attributes[] = {
1063	&sensor_dev_attr_temp2_offset.dev_attr.attr,
1064	NULL
1065};
1066
1067static const struct attribute_group lm90_temp2_offset_group = {
1068	.attrs = lm90_temp2_offset_attributes,
1069};
1070
1071/*
1072 * Additional attributes for devices with emergency sensors
1073 */
1074static SENSOR_DEVICE_ATTR(temp1_emergency, S_IWUSR | S_IRUGO, show_temp8,
1075	set_temp8, LOCAL_EMERG);
1076static SENSOR_DEVICE_ATTR(temp2_emergency, S_IWUSR | S_IRUGO, show_temp8,
1077	set_temp8, REMOTE_EMERG);
1078static SENSOR_DEVICE_ATTR(temp1_emergency_hyst, S_IRUGO, show_temphyst,
1079			  NULL, LOCAL_EMERG);
1080static SENSOR_DEVICE_ATTR(temp2_emergency_hyst, S_IRUGO, show_temphyst,
1081			  NULL, REMOTE_EMERG);
1082
1083static struct attribute *lm90_emergency_attributes[] = {
1084	&sensor_dev_attr_temp1_emergency.dev_attr.attr,
1085	&sensor_dev_attr_temp2_emergency.dev_attr.attr,
1086	&sensor_dev_attr_temp1_emergency_hyst.dev_attr.attr,
1087	&sensor_dev_attr_temp2_emergency_hyst.dev_attr.attr,
1088	NULL
1089};
1090
1091static const struct attribute_group lm90_emergency_group = {
1092	.attrs = lm90_emergency_attributes,
1093};
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1094
1095static SENSOR_DEVICE_ATTR(temp1_emergency_alarm, S_IRUGO, show_alarm, NULL, 15);
1096static SENSOR_DEVICE_ATTR(temp2_emergency_alarm, S_IRUGO, show_alarm, NULL, 13);
 
 
1097
1098static struct attribute *lm90_emergency_alarm_attributes[] = {
1099	&sensor_dev_attr_temp1_emergency_alarm.dev_attr.attr,
1100	&sensor_dev_attr_temp2_emergency_alarm.dev_attr.attr,
1101	NULL
1102};
1103
1104static const struct attribute_group lm90_emergency_alarm_group = {
1105	.attrs = lm90_emergency_alarm_attributes,
1106};
1107
1108/*
1109 * Additional attributes for devices with 3 temperature sensors
1110 */
1111static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp11, NULL,
1112	0, REMOTE2_TEMP);
1113static SENSOR_DEVICE_ATTR_2(temp3_min, S_IWUSR | S_IRUGO, show_temp11,
1114	set_temp11, 3, REMOTE2_LOW);
1115static SENSOR_DEVICE_ATTR_2(temp3_max, S_IWUSR | S_IRUGO, show_temp11,
1116	set_temp11, 4, REMOTE2_HIGH);
1117static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp8,
1118	set_temp8, REMOTE2_CRIT);
1119static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, show_temphyst, NULL,
1120	REMOTE2_CRIT);
1121static SENSOR_DEVICE_ATTR(temp3_emergency, S_IWUSR | S_IRUGO, show_temp8,
1122	set_temp8, REMOTE2_EMERG);
1123static SENSOR_DEVICE_ATTR(temp3_emergency_hyst, S_IRUGO, show_temphyst,
1124			  NULL, REMOTE2_EMERG);
1125
1126static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 9);
1127static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 10);
1128static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 11);
1129static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 12);
1130static SENSOR_DEVICE_ATTR(temp3_emergency_alarm, S_IRUGO, show_alarm, NULL, 14);
1131
1132static struct attribute *lm90_temp3_attributes[] = {
1133	&sensor_dev_attr_temp3_input.dev_attr.attr,
1134	&sensor_dev_attr_temp3_min.dev_attr.attr,
1135	&sensor_dev_attr_temp3_max.dev_attr.attr,
1136	&sensor_dev_attr_temp3_crit.dev_attr.attr,
1137	&sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
1138	&sensor_dev_attr_temp3_emergency.dev_attr.attr,
1139	&sensor_dev_attr_temp3_emergency_hyst.dev_attr.attr,
1140
1141	&sensor_dev_attr_temp3_fault.dev_attr.attr,
1142	&sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
1143	&sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
1144	&sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
1145	&sensor_dev_attr_temp3_emergency_alarm.dev_attr.attr,
1146	NULL
1147};
1148
1149static const struct attribute_group lm90_temp3_group = {
1150	.attrs = lm90_temp3_attributes,
1151};
1152
1153/* pec used for ADM1032 only */
1154static ssize_t show_pec(struct device *dev, struct device_attribute *dummy,
1155			char *buf)
1156{
1157	struct i2c_client *client = to_i2c_client(dev);
1158	return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1159}
1160
1161static ssize_t set_pec(struct device *dev, struct device_attribute *dummy,
1162		       const char *buf, size_t count)
1163{
1164	struct i2c_client *client = to_i2c_client(dev);
1165	long val;
1166	int err;
1167
1168	err = kstrtol(buf, 10, &val);
1169	if (err < 0)
 
 
1170		return err;
1171
1172	switch (val) {
1173	case 0:
1174		client->flags &= ~I2C_CLIENT_PEC;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1175		break;
1176	case 1:
1177		client->flags |= I2C_CLIENT_PEC;
1178		break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1179	default:
1180		return -EINVAL;
1181	}
 
1182
1183	return count;
 
 
 
 
 
 
 
 
 
 
1184}
1185
1186static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec);
 
 
 
 
 
 
 
 
 
 
 
1187
1188/*
1189 * Real code
1190 */
 
 
 
 
 
 
 
 
 
1191
1192/* Return 0 if detection is successful, -ENODEV otherwise */
1193static int lm90_detect(struct i2c_client *client,
1194		       struct i2c_board_info *info)
1195{
1196	struct i2c_adapter *adapter = client->adapter;
1197	int address = client->addr;
1198	const char *name = NULL;
1199	int man_id, chip_id, config1, config2, convrate;
1200
1201	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1202		return -ENODEV;
1203
1204	/* detection and identification */
1205	man_id = i2c_smbus_read_byte_data(client, LM90_REG_R_MAN_ID);
1206	chip_id = i2c_smbus_read_byte_data(client, LM90_REG_R_CHIP_ID);
1207	config1 = i2c_smbus_read_byte_data(client, LM90_REG_R_CONFIG1);
1208	convrate = i2c_smbus_read_byte_data(client, LM90_REG_R_CONVRATE);
1209	if (man_id < 0 || chip_id < 0 || config1 < 0 || convrate < 0)
1210		return -ENODEV;
1211
1212	if (man_id == 0x01 || man_id == 0x5C || man_id == 0x41) {
1213		config2 = i2c_smbus_read_byte_data(client, LM90_REG_R_CONFIG2);
1214		if (config2 < 0)
1215			return -ENODEV;
1216	} else
1217		config2 = 0;		/* Make compiler happy */
1218
1219	if ((address == 0x4C || address == 0x4D)
1220	 && man_id == 0x01) { /* National Semiconductor */
1221		if ((config1 & 0x2A) == 0x00
1222		 && (config2 & 0xF8) == 0x00
1223		 && convrate <= 0x09) {
1224			if (address == 0x4C
1225			 && (chip_id & 0xF0) == 0x20) { /* LM90 */
1226				name = "lm90";
1227			} else
1228			if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
1229				name = "lm99";
1230				dev_info(&adapter->dev,
1231					 "Assuming LM99 chip at 0x%02x\n",
1232					 address);
1233				dev_info(&adapter->dev,
1234					 "If it is an LM89, instantiate it "
1235					 "with the new_device sysfs "
1236					 "interface\n");
1237			} else
1238			if (address == 0x4C
1239			 && (chip_id & 0xF0) == 0x10) { /* LM86 */
1240				name = "lm86";
1241			}
1242		}
1243	} else
1244	if ((address == 0x4C || address == 0x4D)
1245	 && man_id == 0x41) { /* Analog Devices */
1246		if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
1247		 && (config1 & 0x3F) == 0x00
1248		 && convrate <= 0x0A) {
1249			name = "adm1032";
1250			/*
1251			 * The ADM1032 supports PEC, but only if combined
1252			 * transactions are not used.
1253			 */
1254			if (i2c_check_functionality(adapter,
1255						    I2C_FUNC_SMBUS_BYTE))
1256				info->flags |= I2C_CLIENT_PEC;
1257		} else
1258		if (chip_id == 0x51 /* ADT7461 */
1259		 && (config1 & 0x1B) == 0x00
1260		 && convrate <= 0x0A) {
1261			name = "adt7461";
1262		} else
1263		if (chip_id == 0x57 /* ADT7461A, NCT1008 */
1264		 && (config1 & 0x1B) == 0x00
1265		 && convrate <= 0x0A) {
1266			name = "adt7461a";
1267		}
1268	} else
1269	if (man_id == 0x4D) { /* Maxim */
1270		int emerg, emerg2, status2;
1271
1272		/*
1273		 * We read MAX6659_REG_R_REMOTE_EMERG twice, and re-read
1274		 * LM90_REG_R_MAN_ID in between. If MAX6659_REG_R_REMOTE_EMERG
1275		 * exists, both readings will reflect the same value. Otherwise,
1276		 * the readings will be different.
1277		 */
1278		emerg = i2c_smbus_read_byte_data(client,
1279						 MAX6659_REG_R_REMOTE_EMERG);
1280		man_id = i2c_smbus_read_byte_data(client,
1281						  LM90_REG_R_MAN_ID);
1282		emerg2 = i2c_smbus_read_byte_data(client,
1283						  MAX6659_REG_R_REMOTE_EMERG);
1284		status2 = i2c_smbus_read_byte_data(client,
1285						   MAX6696_REG_R_STATUS2);
1286		if (emerg < 0 || man_id < 0 || emerg2 < 0 || status2 < 0)
1287			return -ENODEV;
1288
1289		/*
1290		 * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
1291		 * register. Reading from that address will return the last
1292		 * read value, which in our case is those of the man_id
1293		 * register. Likewise, the config1 register seems to lack a
1294		 * low nibble, so the value will be those of the previous
1295		 * read, so in our case those of the man_id register.
1296		 * MAX6659 has a third set of upper temperature limit registers.
1297		 * Those registers also return values on MAX6657 and MAX6658,
1298		 * thus the only way to detect MAX6659 is by its address.
1299		 * For this reason it will be mis-detected as MAX6657 if its
1300		 * address is 0x4C.
1301		 */
1302		if (chip_id == man_id
1303		 && (address == 0x4C || address == 0x4D || address == 0x4E)
1304		 && (config1 & 0x1F) == (man_id & 0x0F)
1305		 && convrate <= 0x09) {
1306			if (address == 0x4C)
1307				name = "max6657";
1308			else
1309				name = "max6659";
1310		} else
1311		/*
1312		 * Even though MAX6695 and MAX6696 do not have a chip ID
1313		 * register, reading it returns 0x01. Bit 4 of the config1
1314		 * register is unused and should return zero when read. Bit 0 of
1315		 * the status2 register is unused and should return zero when
1316		 * read.
1317		 *
1318		 * MAX6695 and MAX6696 have an additional set of temperature
1319		 * limit registers. We can detect those chips by checking if
1320		 * one of those registers exists.
1321		 */
1322		if (chip_id == 0x01
1323		 && (config1 & 0x10) == 0x00
1324		 && (status2 & 0x01) == 0x00
1325		 && emerg == emerg2
1326		 && convrate <= 0x07) {
1327			name = "max6696";
1328		} else
1329		/*
1330		 * The chip_id register of the MAX6680 and MAX6681 holds the
1331		 * revision of the chip. The lowest bit of the config1 register
1332		 * is unused and should return zero when read, so should the
1333		 * second to last bit of config1 (software reset).
1334		 */
1335		if (chip_id == 0x01
1336		 && (config1 & 0x03) == 0x00
1337		 && convrate <= 0x07) {
1338			name = "max6680";
1339		} else
1340		/*
1341		 * The chip_id register of the MAX6646/6647/6649 holds the
1342		 * revision of the chip. The lowest 6 bits of the config1
1343		 * register are unused and should return zero when read.
1344		 */
1345		if (chip_id == 0x59
1346		 && (config1 & 0x3f) == 0x00
1347		 && convrate <= 0x07) {
1348			name = "max6646";
 
 
 
 
 
 
 
 
 
 
1349		}
1350	} else
1351	if (address == 0x4C
1352	 && man_id == 0x5C) { /* Winbond/Nuvoton */
1353		if ((config1 & 0x2A) == 0x00
1354		 && (config2 & 0xF8) == 0x00) {
1355			if (chip_id == 0x01 /* W83L771W/G */
1356			 && convrate <= 0x09) {
1357				name = "w83l771";
1358			} else
1359			if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */
1360			 && convrate <= 0x08) {
1361				name = "w83l771";
1362			}
1363		}
1364	} else
1365	if (address >= 0x48 && address <= 0x4F
1366	 && man_id == 0xA1) { /*  NXP Semiconductor/Philips */
1367		if (chip_id == 0x00
1368		 && (config1 & 0x2A) == 0x00
1369		 && (config2 & 0xFE) == 0x00
1370		 && convrate <= 0x09) {
1371			name = "sa56004";
1372		}
1373	} else
1374	if ((address == 0x4C || address == 0x4D)
1375	 && man_id == 0x47) { /* GMT */
1376		if (chip_id == 0x01 /* G781 */
1377		 && (config1 & 0x3F) == 0x00
1378		 && convrate <= 0x08)
1379			name = "g781";
1380	} else
1381	if (address == 0x4C
1382	 && man_id == 0x55) { /* Texas Instruments */
1383		int local_ext;
1384
1385		local_ext = i2c_smbus_read_byte_data(client,
1386						     TMP451_REG_R_LOCAL_TEMPL);
1387
1388		if (chip_id == 0x00 /* TMP451 */
1389		 && (config1 & 0x1B) == 0x00
1390		 && convrate <= 0x09
1391		 && (local_ext & 0x0F) == 0x00)
1392			name = "tmp451";
1393	}
1394
1395	if (!name) { /* identification failed */
1396		dev_dbg(&adapter->dev,
1397			"Unsupported chip at 0x%02x (man_id=0x%02X, "
1398			"chip_id=0x%02X)\n", address, man_id, chip_id);
1399		return -ENODEV;
1400	}
1401
1402	strlcpy(info->type, name, I2C_NAME_SIZE);
1403
1404	return 0;
1405}
1406
1407static void lm90_restore_conf(struct i2c_client *client, struct lm90_data *data)
1408{
 
 
 
1409	/* Restore initial configuration */
1410	i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
1411				  data->convrate_orig);
1412	i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
1413				  data->config_orig);
1414}
1415
1416static void lm90_init_client(struct i2c_client *client, struct lm90_data *data)
1417{
1418	u8 config, convrate;
1419
1420	if (lm90_read_reg(client, LM90_REG_R_CONVRATE, &convrate) < 0) {
1421		dev_warn(&client->dev, "Failed to read convrate register!\n");
1422		convrate = LM90_DEF_CONVRATE_RVAL;
1423	}
1424	data->convrate_orig = convrate;
1425
1426	/*
1427	 * Start the conversions.
1428	 */
1429	lm90_set_convrate(client, data, 500);	/* 500ms; 2Hz conversion rate */
1430	if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) {
1431		dev_warn(&client->dev, "Initialization failed!\n");
1432		return;
1433	}
1434	data->config_orig = config;
 
 
 
1435
1436	/* Check Temperature Range Select */
1437	if (data->kind == adt7461 || data->kind == tmp451) {
1438		if (config & 0x04)
1439			data->flags |= LM90_FLAG_ADT7461_EXT;
1440	}
1441
1442	/*
1443	 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
1444	 * 0.125 degree resolution) and range (0x08, extend range
1445	 * to -64 degree) mode for the remote temperature sensor.
1446	 */
1447	if (data->kind == max6680)
1448		config |= 0x18;
1449
1450	/*
 
 
 
 
 
 
 
 
 
1451	 * Select external channel 0 for max6695/96
1452	 */
1453	if (data->kind == max6696)
1454		config &= ~0x08;
1455
1456	config &= 0xBF;	/* run */
1457	if (config != data->config_orig) /* Only write if changed */
1458		i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
 
1459}
1460
1461static bool lm90_is_tripped(struct i2c_client *client, u16 *status)
1462{
1463	struct lm90_data *data = i2c_get_clientdata(client);
1464	u8 st, st2 = 0;
1465
1466	lm90_read_reg(client, LM90_REG_R_STATUS, &st);
 
 
1467
1468	if (data->kind == max6696)
1469		lm90_read_reg(client, MAX6696_REG_R_STATUS2, &st2);
 
 
 
1470
1471	*status = st | (st2 << 8);
1472
1473	if ((st & 0x7f) == 0 && (st2 & 0xfe) == 0)
1474		return false;
1475
1476	if ((st & (LM90_STATUS_LLOW | LM90_STATUS_LHIGH | LM90_STATUS_LTHRM)) ||
1477	    (st2 & MAX6696_STATUS2_LOT2))
1478		dev_warn(&client->dev,
1479			 "temp%d out of range, please check!\n", 1);
1480	if ((st & (LM90_STATUS_RLOW | LM90_STATUS_RHIGH | LM90_STATUS_RTHRM)) ||
1481	    (st2 & MAX6696_STATUS2_ROT2))
1482		dev_warn(&client->dev,
1483			 "temp%d out of range, please check!\n", 2);
1484	if (st & LM90_STATUS_ROPEN)
1485		dev_warn(&client->dev,
1486			 "temp%d diode open, please check!\n", 2);
1487	if (st2 & (MAX6696_STATUS2_R2LOW | MAX6696_STATUS2_R2HIGH |
1488		   MAX6696_STATUS2_R2THRM | MAX6696_STATUS2_R2OT2))
1489		dev_warn(&client->dev,
1490			 "temp%d out of range, please check!\n", 3);
1491	if (st2 & MAX6696_STATUS2_R2OPEN)
1492		dev_warn(&client->dev,
1493			 "temp%d diode open, please check!\n", 3);
1494
1495	return true;
1496}
1497
1498static irqreturn_t lm90_irq_thread(int irq, void *dev_id)
1499{
1500	struct i2c_client *client = dev_id;
1501	u16 status;
1502
1503	if (lm90_is_tripped(client, &status))
1504		return IRQ_HANDLED;
1505	else
1506		return IRQ_NONE;
1507}
1508
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1509static int lm90_probe(struct i2c_client *client,
1510		      const struct i2c_device_id *id)
1511{
1512	struct device *dev = &client->dev;
1513	struct i2c_adapter *adapter = to_i2c_adapter(dev->parent);
 
 
 
1514	struct lm90_data *data;
1515	struct regulator *regulator;
1516	int groups = 0;
1517	int err;
1518
1519	regulator = devm_regulator_get(dev, "vcc");
1520	if (IS_ERR(regulator))
1521		return PTR_ERR(regulator);
1522
1523	err = regulator_enable(regulator);
1524	if (err < 0) {
1525		dev_err(dev, "Failed to enable regulator: %d\n", err);
1526		return err;
1527	}
1528
 
 
 
 
1529	data = devm_kzalloc(dev, sizeof(struct lm90_data), GFP_KERNEL);
1530	if (!data)
1531		return -ENOMEM;
1532
1533	data->client = client;
1534	i2c_set_clientdata(client, data);
1535	mutex_init(&data->update_lock);
1536
1537	data->regulator = regulator;
1538
1539	/* Set the device type */
1540	data->kind = id->driver_data;
 
 
 
1541	if (data->kind == adm1032) {
1542		if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
1543			client->flags &= ~I2C_CLIENT_PEC;
1544	}
1545
1546	/*
1547	 * Different devices have different alarm bits triggering the
1548	 * ALERT# output
1549	 */
1550	data->alert_alarms = lm90_params[data->kind].alert_alarms;
1551
1552	/* Set chip capabilities */
1553	data->flags = lm90_params[data->kind].flags;
1554	data->reg_local_ext = lm90_params[data->kind].reg_local_ext;
1555
1556	/* Set maximum conversion rate */
1557	data->max_convrate = lm90_params[data->kind].max_convrate;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1558
1559	/* Initialize the LM90 chip */
1560	lm90_init_client(client, data);
 
 
 
 
1561
1562	/* Register sysfs hooks */
1563	data->groups[groups++] = &lm90_group;
 
 
1564
1565	if (data->flags & LM90_HAVE_OFFSET)
1566		data->groups[groups++] = &lm90_temp2_offset_group;
 
 
 
 
 
 
 
1567
1568	if (data->flags & LM90_HAVE_EMERGENCY)
1569		data->groups[groups++] = &lm90_emergency_group;
1570
1571	if (data->flags & LM90_HAVE_EMERGENCY_ALARM)
1572		data->groups[groups++] = &lm90_emergency_alarm_group;
1573
1574	if (data->flags & LM90_HAVE_TEMP3)
1575		data->groups[groups++] = &lm90_temp3_group;
 
 
 
 
1576
 
 
 
 
1577	if (client->flags & I2C_CLIENT_PEC) {
1578		err = device_create_file(dev, &dev_attr_pec);
1579		if (err)
1580			goto exit_restore;
 
 
 
1581	}
1582
1583	data->hwmon_dev = hwmon_device_register_with_groups(dev, client->name,
1584							    data, data->groups);
1585	if (IS_ERR(data->hwmon_dev)) {
1586		err = PTR_ERR(data->hwmon_dev);
1587		goto exit_remove_pec;
1588	}
1589
1590	if (client->irq) {
1591		dev_dbg(dev, "IRQ: %d\n", client->irq);
1592		err = devm_request_threaded_irq(dev, client->irq,
1593						NULL, lm90_irq_thread,
1594						IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1595						"lm90", client);
1596		if (err < 0) {
1597			dev_err(dev, "cannot request IRQ %d\n", client->irq);
1598			goto exit_unregister;
1599		}
1600	}
1601
1602	return 0;
1603
1604exit_unregister:
1605	hwmon_device_unregister(data->hwmon_dev);
1606exit_remove_pec:
1607	device_remove_file(dev, &dev_attr_pec);
1608exit_restore:
1609	lm90_restore_conf(client, data);
1610	regulator_disable(data->regulator);
1611
1612	return err;
1613}
1614
1615static int lm90_remove(struct i2c_client *client)
 
1616{
1617	struct lm90_data *data = i2c_get_clientdata(client);
1618
1619	hwmon_device_unregister(data->hwmon_dev);
1620	device_remove_file(&client->dev, &dev_attr_pec);
1621	lm90_restore_conf(client, data);
1622	regulator_disable(data->regulator);
1623
1624	return 0;
1625}
1626
1627static void lm90_alert(struct i2c_client *client, unsigned int flag)
1628{
1629	u16 alarms;
1630
1631	if (lm90_is_tripped(client, &alarms)) {
1632		/*
1633		 * Disable ALERT# output, because these chips don't implement
1634		 * SMBus alert correctly; they should only hold the alert line
1635		 * low briefly.
1636		 */
1637		struct lm90_data *data = i2c_get_clientdata(client);
1638
1639		if ((data->flags & LM90_HAVE_BROKEN_ALERT)
1640		 && (alarms & data->alert_alarms)) {
1641			u8 config;
1642			dev_dbg(&client->dev, "Disabling ALERT#\n");
1643			lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
1644			i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
1645						  config | 0x80);
1646		}
1647	} else {
1648		dev_info(&client->dev, "Everything OK\n");
1649	}
1650}
1651
1652static struct i2c_driver lm90_driver = {
1653	.class		= I2C_CLASS_HWMON,
1654	.driver = {
1655		.name	= "lm90",
 
1656	},
1657	.probe		= lm90_probe,
1658	.remove		= lm90_remove,
1659	.alert		= lm90_alert,
1660	.id_table	= lm90_id,
1661	.detect		= lm90_detect,
1662	.address_list	= normal_i2c,
1663};
1664
1665module_i2c_driver(lm90_driver);
1666
1667MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
1668MODULE_DESCRIPTION("LM90/ADM1032 driver");
1669MODULE_LICENSE("GPL");