#define pr_fmt(fmt) …
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/jiffies.h>
#include <linux/i2c.h>
#include <linux/hwmon.h>
#include <linux/hwmon-vid.h>
#include <linux/hwmon-sysfs.h>
#include <linux/err.h>
#include <linux/mutex.h>
#ifdef CONFIG_ISA
#include <linux/platform_device.h>
#include <linux/ioport.h>
#include <linux/io.h>
#endif
static const unsigned short normal_i2c[] = …;
enum chips { … };
#define LM78_EXTENT …
#define LM78_ADDR_REG_OFFSET …
#define LM78_DATA_REG_OFFSET …
#define LM78_REG_IN_MAX(nr) …
#define LM78_REG_IN_MIN(nr) …
#define LM78_REG_IN(nr) …
#define LM78_REG_FAN_MIN(nr) …
#define LM78_REG_FAN(nr) …
#define LM78_REG_TEMP …
#define LM78_REG_TEMP_OVER …
#define LM78_REG_TEMP_HYST …
#define LM78_REG_ALARM1 …
#define LM78_REG_ALARM2 …
#define LM78_REG_VID_FANDIV …
#define LM78_REG_CONFIG …
#define LM78_REG_CHIPID …
#define LM78_REG_I2C_ADDR …
static inline u8 IN_TO_REG(unsigned long val)
{ … }
#define IN_FROM_REG(val) …
static inline u8 FAN_TO_REG(long rpm, int div)
{ … }
static inline int FAN_FROM_REG(u8 val, int div)
{ … }
static inline s8 TEMP_TO_REG(long val)
{ … }
static inline int TEMP_FROM_REG(s8 val)
{ … }
#define DIV_FROM_REG(val) …
struct lm78_data { … };
static int lm78_read_value(struct lm78_data *data, u8 reg);
static int lm78_write_value(struct lm78_data *data, u8 reg, u8 value);
static struct lm78_data *lm78_update_device(struct device *dev);
static void lm78_init_device(struct lm78_data *data);
static ssize_t in_show(struct device *dev, struct device_attribute *da,
char *buf)
{ … }
static ssize_t in_min_show(struct device *dev, struct device_attribute *da,
char *buf)
{ … }
static ssize_t in_max_show(struct device *dev, struct device_attribute *da,
char *buf)
{ … }
static ssize_t in_min_store(struct device *dev, struct device_attribute *da,
const char *buf, size_t count)
{ … }
static ssize_t in_max_store(struct device *dev, struct device_attribute *da,
const char *buf, size_t count)
{ … }
static SENSOR_DEVICE_ATTR_RO(in0_input, in, 0);
static SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0);
static SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0);
static SENSOR_DEVICE_ATTR_RO(in1_input, in, 1);
static SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1);
static SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1);
static SENSOR_DEVICE_ATTR_RO(in2_input, in, 2);
static SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2);
static SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2);
static SENSOR_DEVICE_ATTR_RO(in3_input, in, 3);
static SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3);
static SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3);
static SENSOR_DEVICE_ATTR_RO(in4_input, in, 4);
static SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4);
static SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4);
static SENSOR_DEVICE_ATTR_RO(in5_input, in, 5);
static SENSOR_DEVICE_ATTR_RW(in5_min, in_min, 5);
static SENSOR_DEVICE_ATTR_RW(in5_max, in_max, 5);
static SENSOR_DEVICE_ATTR_RO(in6_input, in, 6);
static SENSOR_DEVICE_ATTR_RW(in6_min, in_min, 6);
static SENSOR_DEVICE_ATTR_RW(in6_max, in_max, 6);
static ssize_t temp1_input_show(struct device *dev,
struct device_attribute *da, char *buf)
{ … }
static ssize_t temp1_max_show(struct device *dev, struct device_attribute *da,
char *buf)
{ … }
static ssize_t temp1_max_store(struct device *dev,
struct device_attribute *da, const char *buf,
size_t count)
{ … }
static ssize_t temp1_max_hyst_show(struct device *dev,
struct device_attribute *da, char *buf)
{ … }
static ssize_t temp1_max_hyst_store(struct device *dev,
struct device_attribute *da,
const char *buf, size_t count)
{ … }
static DEVICE_ATTR_RO(temp1_input);
static DEVICE_ATTR_RW(temp1_max);
static DEVICE_ATTR_RW(temp1_max_hyst);
static ssize_t fan_show(struct device *dev, struct device_attribute *da,
char *buf)
{ … }
static ssize_t fan_min_show(struct device *dev, struct device_attribute *da,
char *buf)
{ … }
static ssize_t fan_min_store(struct device *dev, struct device_attribute *da,
const char *buf, size_t count)
{ … }
static ssize_t fan_div_show(struct device *dev, struct device_attribute *da,
char *buf)
{ … }
static ssize_t fan_div_store(struct device *dev, struct device_attribute *da,
const char *buf, size_t count)
{ … }
static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
static SENSOR_DEVICE_ATTR_RO(fan3_input, fan, 2);
static SENSOR_DEVICE_ATTR_RW(fan3_min, fan_min, 2);
static SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
static SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
static SENSOR_DEVICE_ATTR_RO(fan3_div, fan_div, 2);
static ssize_t cpu0_vid_show(struct device *dev, struct device_attribute *da,
char *buf)
{ … }
static DEVICE_ATTR_RO(cpu0_vid);
static ssize_t alarms_show(struct device *dev, struct device_attribute *da,
char *buf)
{ … }
static DEVICE_ATTR_RO(alarms);
static ssize_t alarm_show(struct device *dev, struct device_attribute *da,
char *buf)
{ … }
static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0);
static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1);
static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2);
static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 8);
static SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm, 9);
static SENSOR_DEVICE_ATTR_RO(in6_alarm, alarm, 10);
static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 6);
static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 7);
static SENSOR_DEVICE_ATTR_RO(fan3_alarm, alarm, 11);
static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 4);
static struct attribute *lm78_attrs[] = …;
ATTRIBUTE_GROUPS(…);
#ifdef CONFIG_ISA
static struct platform_device *pdev;
static unsigned short isa_address = 0x290;
static struct lm78_data *lm78_data_if_isa(void)
{
return pdev ? platform_get_drvdata(pdev) : NULL;
}
static int lm78_alias_detect(struct i2c_client *client, u8 chipid)
{
struct lm78_data *isa;
int i;
if (!pdev)
return 0;
isa = platform_get_drvdata(pdev);
if (lm78_read_value(isa, LM78_REG_I2C_ADDR) != client->addr)
return 0;
if ((lm78_read_value(isa, LM78_REG_CHIPID) & 0xfe) != (chipid & 0xfe))
return 0;
for (i = 0x2b; i <= 0x3d; i++) {
if (lm78_read_value(isa, i) !=
i2c_smbus_read_byte_data(client, i))
return 0;
}
if (lm78_read_value(isa, LM78_REG_CONFIG) !=
i2c_smbus_read_byte_data(client, LM78_REG_CONFIG))
return 0;
for (i = 0x43; i <= 0x46; i++) {
if (lm78_read_value(isa, i) !=
i2c_smbus_read_byte_data(client, i))
return 0;
}
return 1;
}
#else
static int lm78_alias_detect(struct i2c_client *client, u8 chipid)
{ … }
static struct lm78_data *lm78_data_if_isa(void)
{ … }
#endif
static int lm78_i2c_detect(struct i2c_client *client,
struct i2c_board_info *info)
{ … }
static int lm78_i2c_probe(struct i2c_client *client)
{ … }
static const struct i2c_device_id lm78_i2c_id[] = …;
MODULE_DEVICE_TABLE(i2c, lm78_i2c_id);
static struct i2c_driver lm78_driver = …;
static int lm78_read_value(struct lm78_data *data, u8 reg)
{ … }
static int lm78_write_value(struct lm78_data *data, u8 reg, u8 value)
{ … }
static void lm78_init_device(struct lm78_data *data)
{ … }
static struct lm78_data *lm78_update_device(struct device *dev)
{ … }
#ifdef CONFIG_ISA
static int lm78_isa_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct device *hwmon_dev;
struct lm78_data *data;
struct resource *res;
res = platform_get_resource(pdev, IORESOURCE_IO, 0);
if (!devm_request_region(dev, res->start + LM78_ADDR_REG_OFFSET,
2, "lm78"))
return -EBUSY;
data = devm_kzalloc(dev, sizeof(struct lm78_data), GFP_KERNEL);
if (!data)
return -ENOMEM;
mutex_init(&data->lock);
data->isa_addr = res->start;
platform_set_drvdata(pdev, data);
if (lm78_read_value(data, LM78_REG_CHIPID) & 0x80) {
data->type = lm79;
data->name = "lm79";
} else {
data->type = lm78;
data->name = "lm78";
}
lm78_init_device(data);
hwmon_dev = devm_hwmon_device_register_with_groups(dev, data->name,
data, lm78_groups);
return PTR_ERR_OR_ZERO(hwmon_dev);
}
static struct platform_driver lm78_isa_driver = {
.driver = {
.name = "lm78",
},
.probe = lm78_isa_probe,
};
static int __init lm78_isa_found(unsigned short address)
{
int val, save, found = 0;
int port;
for (port = address; port < address + LM78_EXTENT; port++) {
if (!request_region(port, 1, "lm78")) {
pr_debug("Failed to request port 0x%x\n", port);
goto release;
}
}
#define REALLY_SLOW_IO
val = inb_p(address + 1);
if (inb_p(address + 2) != val
|| inb_p(address + 3) != val
|| inb_p(address + 7) != val)
goto release;
#undef REALLY_SLOW_IO
save = inb_p(address + LM78_ADDR_REG_OFFSET);
if (save & 0x80)
goto release;
val = ~save & 0x7f;
outb_p(val, address + LM78_ADDR_REG_OFFSET);
if (inb_p(address + LM78_ADDR_REG_OFFSET) != (val | 0x80)) {
outb_p(save, address + LM78_ADDR_REG_OFFSET);
goto release;
}
outb_p(LM78_REG_CONFIG, address + LM78_ADDR_REG_OFFSET);
val = inb_p(address + LM78_DATA_REG_OFFSET);
if (val & 0x80)
goto release;
outb_p(LM78_REG_I2C_ADDR, address + LM78_ADDR_REG_OFFSET);
val = inb_p(address + LM78_DATA_REG_OFFSET);
if (val < 0x03 || val > 0x77)
goto release;
if (inb_p(address + LM78_ADDR_REG_OFFSET) & 0x80)
goto release;
outb_p(0x4f, address + LM78_ADDR_REG_OFFSET);
val = inb_p(address + LM78_DATA_REG_OFFSET);
if (val == 0xa3 || val == 0x5c)
goto release;
outb_p(0x58, address + LM78_ADDR_REG_OFFSET);
val = inb_p(address + LM78_DATA_REG_OFFSET);
if (val == 0x90)
goto release;
outb_p(LM78_REG_CHIPID, address + LM78_ADDR_REG_OFFSET);
val = inb_p(address + LM78_DATA_REG_OFFSET);
if (val == 0x00 || val == 0x20
|| val == 0x40
|| (val & 0xfe) == 0xc0)
found = 1;
if (found)
pr_info("Found an %s chip at %#x\n",
val & 0x80 ? "LM79" : "LM78", (int)address);
release:
for (port--; port >= address; port--)
release_region(port, 1);
return found;
}
static int __init lm78_isa_device_add(unsigned short address)
{
struct resource res = {
.start = address,
.end = address + LM78_EXTENT - 1,
.name = "lm78",
.flags = IORESOURCE_IO,
};
int err;
pdev = platform_device_alloc("lm78", address);
if (!pdev) {
err = -ENOMEM;
pr_err("Device allocation failed\n");
goto exit;
}
err = platform_device_add_resources(pdev, &res, 1);
if (err) {
pr_err("Device resource addition failed (%d)\n", err);
goto exit_device_put;
}
err = platform_device_add(pdev);
if (err) {
pr_err("Device addition failed (%d)\n", err);
goto exit_device_put;
}
return 0;
exit_device_put:
platform_device_put(pdev);
exit:
pdev = NULL;
return err;
}
static int __init lm78_isa_register(void)
{
int res;
if (lm78_isa_found(isa_address)) {
res = platform_driver_register(&lm78_isa_driver);
if (res)
goto exit;
res = lm78_isa_device_add(isa_address);
if (res)
goto exit_unreg_isa_driver;
}
return 0;
exit_unreg_isa_driver:
platform_driver_unregister(&lm78_isa_driver);
exit:
return res;
}
static void lm78_isa_unregister(void)
{
if (pdev) {
platform_device_unregister(pdev);
platform_driver_unregister(&lm78_isa_driver);
}
}
#else
static int __init lm78_isa_register(void)
{ … }
static void lm78_isa_unregister(void)
{ … }
#endif
static int __init sm_lm78_init(void)
{ … }
static void __exit sm_lm78_exit(void)
{ … }
MODULE_AUTHOR(…) …;
MODULE_DESCRIPTION(…) …;
MODULE_LICENSE(…) …;
module_init(…) …;
module_exit(sm_lm78_exit);