1,318
社区成员
发帖
与我相关
我的任务
分享
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/sysfs.h>
#include <linux/ctype.h>
#include <linux/hwmon-sysfs.h>
/* Device registers */
#define at24c08_REG_CONFIG 0x00
#define at24c08_REG_ALARM 0x01
#define at24c08_REG_ELAPSED 0x05
#define at24c08_REG_EVT_CNTR 0x09
#define at24c08_REG_EEPROM 0x0b
#define at24c08_REG_RESET 0x1d
#define at24c08_REG_WRITE_DISABLE 0x1e
#define at24c08_REG_WRITE_MEM_DISABLE 0x1f
#define at24c08_EEPROM_SIZE 10
/*
* Generic counter attributes
*/
static ssize_t at24c08_show(struct device *dev, struct device_attribute *attr,
char *buf)
{
struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
struct i2c_client *client = to_i2c_client(dev);
__le32 val = 0;
int rc;
dev_dbg(dev, "at24c08_show() called on %s\n", attr->attr.name);
/* Read the register */
rc = i2c_smbus_read_i2c_block_data(client, sattr->index, sattr->nr,
(u8 *) & val);
if (rc < 0)
return -EIO;
/* Special case: the 32 bit regs are time values with 1/4s
* resolution, scale them up to milliseconds */
if (sattr->nr == 4)
return sprintf(buf, "%llu\n",
((unsigned long long)le32_to_cpu(val)) * 250);
/* Format the output string and return # of bytes */
return sprintf(buf, "%li\n", (long)le32_to_cpu(val));
}
static ssize_t at24c08_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
struct i2c_client *client = to_i2c_client(dev);
char *endp;
u64 val;
__le32 val_le;
int rc;
dev_dbg(dev, "at24c08_store() called on %s\n", attr->attr.name);
/* Decode input */
val = simple_strtoull(buf, &endp, 0);
if (buf == endp) {
dev_dbg(dev, "input string not a number\n");
return -EINVAL;
}
/* Special case: the 32 bit regs are time values with 1/4s
* resolution, scale input down to quarter-seconds */
if (sattr->nr == 4)
do_div(val, 250);
/* write out the value */
val_le = cpu_to_le32(val);
rc = i2c_smbus_write_i2c_block_data(client, sattr->index, sattr->nr,
(u8 *) & val_le);
if (rc < 0) {
dev_err(dev, "register write failed; reg=0x%x, size=%i\n",
sattr->index, sattr->nr);
return -EIO;
}
return count;
}
/*
* Simple register attributes
*/
static SENSOR_DEVICE_ATTR_2(elapsed_time, S_IRUGO | S_IWUSR, at24c08_show,
at24c08_store, 4, at24c08_REG_ELAPSED);
static SENSOR_DEVICE_ATTR_2(alarm_time, S_IRUGO | S_IWUSR, at24c08_show,
at24c08_store, 4, at24c08_REG_ALARM);
static SENSOR_DEVICE_ATTR_2(event_count, S_IRUGO | S_IWUSR, at24c08_show,
at24c08_store, 2, at24c08_REG_EVT_CNTR);
static const struct attribute_group at24c08_group = {
.attrs = (struct attribute *[]) {
&sensor_dev_attr_elapsed_time.dev_attr.attr,
&sensor_dev_attr_alarm_time.dev_attr.attr,
&sensor_dev_attr_event_count.dev_attr.attr,
NULL,
},
};
/*
* User data attribute
*/
static ssize_t at24c08_eeprom_read(struct kobject *kobj, struct bin_attribute *attr,
char *buf, loff_t off, size_t count)
{
struct i2c_client *client = kobj_to_i2c_client(kobj);
int rc;
dev_dbg(&client->dev, "at24c08_eeprom_read(p=%p, off=%lli, c=%zi)\n",
buf, off, count);
if (off >= at24c08_EEPROM_SIZE)
return 0;
if (off + count > at24c08_EEPROM_SIZE)
count = at24c08_EEPROM_SIZE - off;
rc = i2c_smbus_read_i2c_block_data(client, at24c08_REG_EEPROM + off,
count, buf);
if (rc < 0)
return -EIO;
return count;
}
static ssize_t at24c08_eeprom_write(struct kobject *kobj, struct bin_attribute *attr,
char *buf, loff_t off, size_t count)
{
struct i2c_client *client = kobj_to_i2c_client(kobj);
dev_dbg(&client->dev, "at24c08_eeprom_write(p=%p, off=%lli, c=%zi)\n",
buf, off, count);
if (off >= at24c08_EEPROM_SIZE)
return -ENOSPC;
if (off + count > at24c08_EEPROM_SIZE)
count = at24c08_EEPROM_SIZE - off;
/* Write out to the device */
if (i2c_smbus_write_i2c_block_data(client, at24c08_REG_EEPROM + off,
count, buf) < 0)
return -EIO;
return count;
}
static struct bin_attribute at24c08_eeprom_attr = {
.attr = {
.name = "eeprom",
.mode = S_IRUGO | S_IWUSR,
},
.size = at24c08_EEPROM_SIZE,
.read = at24c08_eeprom_read,
.write = at24c08_eeprom_write,
};
/*
* Called when a at24c08 device is matched with this driver
*/
static int at24c08_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
int rc;
if (!i2c_check_functionality(client->adapter,
I2C_FUNC_SMBUS_I2C_BLOCK)) {
dev_err(&client->dev, "i2c bus does not support the at24c08\n");
rc = -ENODEV;
goto exit;
}
rc = sysfs_create_group(&client->dev.kobj, &at24c08_group);
if (rc)
goto exit;
rc = sysfs_create_bin_file(&client->dev.kobj, &at24c08_eeprom_attr);
if (rc)
goto exit_bin_attr;
return 0;
exit_bin_attr:
sysfs_remove_group(&client->dev.kobj, &at24c08_group);
exit:
return rc;
}
static int at24c08_remove(struct i2c_client *client)
{
sysfs_remove_bin_file(&client->dev.kobj, &at24c08_eeprom_attr);
sysfs_remove_group(&client->dev.kobj, &at24c08_group);
return 0;
}
static const struct i2c_device_id at24c08_id[] = {
{ "at24c08", 0 },
{ }
};
MODULE_DEVICE_TABLE(i2c, at24c08_id);
static struct i2c_driver at24c08_driver = {
.driver = {
.name = "at24c08",
},
.probe = at24c08_probe,
.remove = at24c08_remove,
.id_table = at24c08_id,
};
static int __init at24c08_init(void)
{
return i2c_add_driver(&at24c08_driver);
}
static void __exit at24c08_exit(void)
{
i2c_del_driver(&at24c08_driver);
}
MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
MODULE_DESCRIPTION("at24c08 Elapsed Time Indicator driver");
MODULE_LICENSE("GPL");
module_init(at24c08_init);
module_exit(at24c08_exit);