| 1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
/* |
| 3 |
* ADT7410/ADT7420 digital temperature sensor driver |
| 4 |
* |
| 5 |
* Copyright 2012-2013 Analog Devices Inc. |
| 6 |
* Author: Lars-Peter Clausen <[email protected]> |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <linux/module.h> |
| 10 |
#include <linux/mod_devicetable.h> |
| 11 |
#include <linux/init.h> |
| 12 |
#include <linux/i2c.h> |
| 13 |
#include <linux/regmap.h> |
| 14 |
|
| 15 |
#include "adt7x10.h" |
| 16 |
|
| 17 |
static bool adt7410_regmap_is_volatile(struct device *dev, unsigned int reg) |
| 18 |
{ |
| 19 |
switch (reg) { |
| 20 |
case ADT7X10_TEMPERATURE: |
| 21 |
case ADT7X10_STATUS: |
| 22 |
return true; |
| 23 |
default: |
| 24 |
return false; |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
static int adt7410_reg_read(void *context, unsigned int reg, unsigned int *val) |
| 29 |
{ |
| 30 |
struct i2c_client *client = context; |
| 31 |
int regval; |
| 32 |
|
| 33 |
switch (reg) { |
| 34 |
case ADT7X10_TEMPERATURE: |
| 35 |
case ADT7X10_T_ALARM_HIGH: |
| 36 |
case ADT7X10_T_ALARM_LOW: |
| 37 |
case ADT7X10_T_CRIT: |
| 38 |
regval = i2c_smbus_read_word_swapped(client, reg); |
| 39 |
break; |
| 40 |
default: |
| 41 |
regval = i2c_smbus_read_byte_data(client, reg); |
| 42 |
break; |
| 43 |
} |
| 44 |
if (regval < 0) |
| 45 |
return regval; |
| 46 |
*val = regval; |
| 47 |
return 0; |
| 48 |
} |
| 49 |
|
| 50 |
static int adt7410_reg_write(void *context, unsigned int reg, unsigned int val) |
| 51 |
{ |
| 52 |
struct i2c_client *client = context; |
| 53 |
int ret; |
| 54 |
|
| 55 |
switch (reg) { |
| 56 |
case ADT7X10_TEMPERATURE: |
| 57 |
case ADT7X10_T_ALARM_HIGH: |
| 58 |
case ADT7X10_T_ALARM_LOW: |
| 59 |
case ADT7X10_T_CRIT: |
| 60 |
ret = i2c_smbus_write_word_swapped(client, reg, val); |
| 61 |
break; |
| 62 |
default: |
| 63 |
ret = i2c_smbus_write_byte_data(client, reg, val); |
| 64 |
break; |
| 65 |
} |
| 66 |
return ret; |
| 67 |
} |
| 68 |
|
| 69 |
static const struct regmap_config adt7410_regmap_config = { |
| 70 |
.reg_bits = 8, |
| 71 |
.val_bits = 16, |
| 72 |
.max_register = ADT7X10_ID, |
| 73 |
.cache_type = REGCACHE_MAPLE, |
| 74 |
.volatile_reg = adt7410_regmap_is_volatile, |
| 75 |
.reg_read = adt7410_reg_read, |
| 76 |
.reg_write = adt7410_reg_write, |
| 77 |
}; |
| 78 |
|
| 79 |
static int adt7410_i2c_probe(struct i2c_client *client) |
| 80 |
{ |
| 81 |
struct regmap *regmap; |
| 82 |
|
| 83 |
regmap = devm_regmap_init(&client->dev, NULL, client, |
| 84 |
&adt7410_regmap_config); |
| 85 |
if (IS_ERR(regmap)) |
| 86 |
return PTR_ERR(regmap); |
| 87 |
|
| 88 |
return adt7x10_probe(&client->dev, client->name, client->irq, regmap); |
| 89 |
} |
| 90 |
|
| 91 |
static const struct i2c_device_id adt7410_ids[] = { |
| 92 |
{ "adt7410" }, |
| 93 |
{ "adt7420" }, |
| 94 |
{ "adt7422" }, |
| 95 |
{} |
| 96 |
}; |
| 97 |
MODULE_DEVICE_TABLE(i2c, adt7410_ids); |
| 98 |
|
| 99 |
static const struct of_device_id adt7410_of_match[] = { |
| 100 |
{ .compatible = "adi,adt7410" }, |
| 101 |
{ .compatible = "adi,adt7420" }, |
| 102 |
{ .compatible = "adi,adt7422" }, |
| 103 |
{ } |
| 104 |
}; |
| 105 |
MODULE_DEVICE_TABLE(of, adt7410_of_match); |
| 106 |
|
| 107 |
static struct i2c_driver adt7410_driver = { |
| 108 |
.driver = { |
| 109 |
.name = "adt7410", |
| 110 |
.pm = pm_sleep_ptr(&adt7x10_dev_pm_ops), |
| 111 |
.of_match_table = adt7410_of_match, |
| 112 |
}, |
| 113 |
.probe = adt7410_i2c_probe, |
| 114 |
.id_table = adt7410_ids, |
| 115 |
}; |
| 116 |
module_i2c_driver(adt7410_driver); |
| 117 |
|
| 118 |
MODULE_AUTHOR("Lars-Peter Clausen <[email protected]>"); |
| 119 |
MODULE_DESCRIPTION("ADT7410/AD7420 driver"); |
| 120 |
MODULE_LICENSE("GPL"); |