blob: 3d1d55157e5f3b605bb4695c797eb12a6987bfff [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Arce, Abraham110f4222010-05-13 15:04:30 -05002 * Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and
3 * Philip Edelbrock <phil@netroedge.com>
4 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
5 * Copyright (C) 2003 IBM Corp.
Jean Delvare7c81c602014-01-29 20:40:08 +01006 * Copyright (C) 2004 Jean Delvare <jdelvare@suse.de>
Arce, Abraham110f4222010-05-13 15:04:30 -05007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/module.h>
Himangi Saraogi1698da22014-08-02 00:34:23 +053021#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/jiffies.h>
23#include <linux/i2c.h>
Ingo Molnar5c085d32006-01-18 23:16:04 +010024#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26/* Addresses to scan */
Jean Delvare2cdddeb2008-01-27 18:14:47 +010027static const unsigned short normal_i2c[] = { 0x50, 0x51, 0x52, 0x53, 0x54,
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 0x55, 0x56, 0x57, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31/* Size of EEPROM in bytes */
32#define EEPROM_SIZE 256
33
34/* possible types of eeprom devices */
35enum eeprom_nature {
36 UNKNOWN,
37 VAIO,
38};
39
40/* Each client has this additional data */
41struct eeprom_data {
Ingo Molnar5c085d32006-01-18 23:16:04 +010042 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 u8 valid; /* bitfield, bit!=0 if slice is valid */
44 unsigned long last_updated[8]; /* In jiffies, 8 slices */
45 u8 data[EEPROM_SIZE]; /* Register values */
46 enum eeprom_nature nature;
47};
48
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static void eeprom_update_client(struct i2c_client *client, u8 slice)
51{
52 struct eeprom_data *data = i2c_get_clientdata(client);
Jean Delvare1b4dff92008-07-14 22:38:29 +020053 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Ingo Molnar5c085d32006-01-18 23:16:04 +010055 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57 if (!(data->valid & (1 << slice)) ||
58 time_after(jiffies, data->last_updated[slice] + 300 * HZ)) {
59 dev_dbg(&client->dev, "Starting eeprom update, slice %u\n", slice);
60
61 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
Jean Delvare4b2643d2007-07-12 14:12:29 +020062 for (i = slice << 5; i < (slice + 1) << 5; i += 32)
63 if (i2c_smbus_read_i2c_block_data(client, i,
64 32, data->data + i)
65 != 32)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 goto exit;
67 } else {
Jean Delvare1b4dff92008-07-14 22:38:29 +020068 for (i = slice << 5; i < (slice + 1) << 5; i += 2) {
69 int word = i2c_smbus_read_word_data(client, i);
70 if (word < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 goto exit;
Jean Delvare1b4dff92008-07-14 22:38:29 +020072 data->data[i] = word & 0xff;
73 data->data[i + 1] = word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 }
75 }
76 data->last_updated[slice] = jiffies;
77 data->valid |= (1 << slice);
78 }
79exit:
Ingo Molnar5c085d32006-01-18 23:16:04 +010080 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
Chris Wright2c3c8be2010-05-12 18:28:57 -070083static ssize_t eeprom_read(struct file *filp, struct kobject *kobj,
84 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +080085 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Geliang Tang092462c2016-01-13 23:30:11 +080087 struct i2c_client *client = to_i2c_client(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 struct eeprom_data *data = i2c_get_clientdata(client);
89 u8 slice;
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 /* Only refresh slices which contain requested bytes */
92 for (slice = off >> 5; slice <= (off + count - 1) >> 5; slice++)
93 eeprom_update_client(client, slice);
94
Jean Delvare0f2cbd32007-11-15 19:24:03 +010095 /* Hide Vaio private settings to regular users:
96 - BIOS passwords: bytes 0x00 to 0x0f
97 - UUID: bytes 0x10 to 0x1f
98 - Serial number: 0xc0 to 0xdf */
99 if (data->nature == VAIO && !capable(CAP_SYS_ADMIN)) {
100 int i;
101
102 for (i = 0; i < count; i++) {
103 if ((off + i <= 0x1f) ||
104 (off + i >= 0xc0 && off + i <= 0xdf))
105 buf[i] = 0;
106 else
107 buf[i] = data->data[off + i];
108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 } else {
110 memcpy(buf, &data->data[off], count);
111 }
112
113 return count;
114}
115
116static struct bin_attribute eeprom_attr = {
117 .attr = {
118 .name = "eeprom",
119 .mode = S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 },
121 .size = EEPROM_SIZE,
122 .read = eeprom_read,
123};
124
Jean Delvare68be3362008-07-16 19:30:05 +0200125/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100126static int eeprom_detect(struct i2c_client *client, struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Jean Delvare68be3362008-07-16 19:30:05 +0200128 struct i2c_adapter *adapter = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Jean Delvared4653bf2008-07-14 22:38:29 +0200130 /* EDID EEPROMs are often 24C00 EEPROMs, which answer to all
131 addresses 0x50-0x57, but we only care about 0x50. So decline
132 attaching to addresses >= 0x51 on DDC buses */
Jean Delvare68be3362008-07-16 19:30:05 +0200133 if (!(adapter->class & I2C_CLASS_SPD) && client->addr >= 0x51)
134 return -ENODEV;
Jean Delvared4653bf2008-07-14 22:38:29 +0200135
Jean Delvare1b4dff92008-07-14 22:38:29 +0200136 /* There are four ways we can read the EEPROM data:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 (1) I2C block reads (faster, but unsupported by most adapters)
Jean Delvare1b4dff92008-07-14 22:38:29 +0200138 (2) Word reads (128% overhead)
139 (3) Consecutive byte reads (88% overhead, unsafe)
140 (4) Regular byte data reads (265% overhead)
141 The third and fourth methods are not implemented by this driver
142 because all known adapters support one of the first two. */
143 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)
144 && !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
Jean Delvare68be3362008-07-16 19:30:05 +0200145 return -ENODEV;
146
147 strlcpy(info->type, "eeprom", I2C_NAME_SIZE);
148
149 return 0;
150}
151
152static int eeprom_probe(struct i2c_client *client,
153 const struct i2c_device_id *id)
154{
155 struct i2c_adapter *adapter = client->adapter;
156 struct eeprom_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Himangi Saraogi1698da22014-08-02 00:34:23 +0530158 data = devm_kzalloc(&client->dev, sizeof(struct eeprom_data),
159 GFP_KERNEL);
160 if (!data)
161 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 memset(data->data, 0xff, EEPROM_SIZE);
Jean Delvaref741f672008-07-14 22:38:36 +0200164 i2c_set_clientdata(client, data);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100165 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 data->nature = UNKNOWN;
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 /* Detect the Vaio nature of EEPROMs.
Jean Delvare8b925a32007-11-15 19:24:03 +0100169 We use the "PCG-" or "VGN-" prefix as the signature. */
Jean Delvare68be3362008-07-16 19:30:05 +0200170 if (client->addr == 0x57
Jean Delvare1b4dff92008-07-14 22:38:29 +0200171 && i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
Jean Delvare8b925a32007-11-15 19:24:03 +0100172 char name[4];
173
Jean Delvaref741f672008-07-14 22:38:36 +0200174 name[0] = i2c_smbus_read_byte_data(client, 0x80);
175 name[1] = i2c_smbus_read_byte_data(client, 0x81);
176 name[2] = i2c_smbus_read_byte_data(client, 0x82);
177 name[3] = i2c_smbus_read_byte_data(client, 0x83);
Jean Delvare8b925a32007-11-15 19:24:03 +0100178
179 if (!memcmp(name, "PCG-", 4) || !memcmp(name, "VGN-", 4)) {
Jean Delvaref741f672008-07-14 22:38:36 +0200180 dev_info(&client->dev, "Vaio EEPROM detected, "
Jean Delvare0f2cbd32007-11-15 19:24:03 +0100181 "enabling privacy protection\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 data->nature = VAIO;
183 }
184 }
185
186 /* create the sysfs eeprom file */
Himangi Saraogi1698da22014-08-02 00:34:23 +0530187 return sysfs_create_bin_file(&client->dev.kobj, &eeprom_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
Jean Delvare68be3362008-07-16 19:30:05 +0200190static int eeprom_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Jean Delvare7d9db672006-09-03 22:20:24 +0200192 sysfs_remove_bin_file(&client->dev.kobj, &eeprom_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 return 0;
195}
196
Jean Delvare68be3362008-07-16 19:30:05 +0200197static const struct i2c_device_id eeprom_id[] = {
198 { "eeprom", 0 },
199 { }
200};
201
202static struct i2c_driver eeprom_driver = {
203 .driver = {
204 .name = "eeprom",
205 },
206 .probe = eeprom_probe,
207 .remove = eeprom_remove,
208 .id_table = eeprom_id,
209
210 .class = I2C_CLASS_DDC | I2C_CLASS_SPD,
211 .detect = eeprom_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100212 .address_list = normal_i2c,
Jean Delvare68be3362008-07-16 19:30:05 +0200213};
214
Axel Lina64fe2e2012-01-22 15:36:45 +0800215module_i2c_driver(eeprom_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
218 "Philip Edelbrock <phil@netroedge.com> and "
219 "Greg Kroah-Hartman <greg@kroah.com>");
220MODULE_DESCRIPTION("I2C EEPROM driver");
221MODULE_LICENSE("GPL");