root/lm-sensors/trunk/kernel/chips/eeprom.c @ 207

Revision 207, 10.7 KB (checked in by frodo, 14 years ago)

Adrian's copyright patch applied

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2    eeprom.c - Part of lm_sensors, Linux kernel modules for hardware
3               monitoring
4    Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl> and
5    Philip Edelbrock <phil@netroedge.com>
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20*/
21
22#include <linux/module.h>
23#include <linux/malloc.h>
24#include "smbus.h"
25#include "sensors.h"
26#include "i2c.h"
27#include "version.h"
28
29/* Many constants specified below */
30
31/* EEPROM memory types: */
32#define ONE_K           1
33#define TWO_K           2
34#define FOUR_K          3
35#define EIGHT_K         4
36#define SIXTEEN_K       5
37
38/* Conversions */
39/* Size of EEPROM in bytes */
40#define EEPROM_SIZE 128
41
42/* Each client has this additional data */
43struct eeprom_data {
44         int sysctl_id;
45
46         struct semaphore update_lock;
47         char valid;                 /* !=0 if following fields are valid */
48         unsigned long last_updated; /* In jiffies */
49
50         u8 data[EEPROM_SIZE]; /* Register values */
51         int memtype;
52};
53
54#ifdef MODULE
55extern int init_module(void);
56extern int cleanup_module(void);
57#endif /* MODULE */
58
59static int eeprom_init(void);
60static int eeprom_cleanup(void);
61
62static int eeprom_attach_adapter(struct i2c_adapter *adapter);
63static int eeprom_detach_client(struct i2c_client *client);
64static int eeprom_command(struct i2c_client *client, unsigned int cmd,
65                        void *arg);
66                       
67static void eeprom_inc_use (struct i2c_client *client);
68static void eeprom_dec_use (struct i2c_client *client);
69
70static u16 swap_bytes(u16 val);
71
72static int eeprom_read_value(struct i2c_client *client, u8 reg);
73static int eeprom_write_value(struct i2c_client *client, u8 reg, u16 value);
74
75static void eeprom_contents(struct i2c_client *client, int operation, int ctl_name,
76                      int *nrels_mag, long *results);
77static void eeprom_update_client(struct i2c_client *client);
78
79
80/* This is the driver that will be inserted */
81static struct i2c_driver eeprom_driver = {
82  /* name */            "EEPROM READER",
83  /* id */              I2C_DRIVERID_EEPROM,
84  /* flags */           DF_NOTIFY,
85  /* attach_adapter */  &eeprom_attach_adapter,
86  /* detach_client */   &eeprom_detach_client,
87  /* command */         &eeprom_command,
88  /* inc_use */         &eeprom_inc_use,
89  /* dec_use */         &eeprom_dec_use
90};
91
92/* These files are created for each detected EEPROM. This is just a template;
93   though at first sight, you might think we could use a statically
94   allocated list, we need some way to get back to the parent - which
95   is done through one of the 'extra' fields which are initialized
96   when a new copy is allocated. */
97static ctl_table eeprom_dir_table_template[] = {
98  { EEPROM_SYSCTL1, "data0-15", NULL, 0, 0644, NULL, &sensors_proc_real,
99    &sensors_sysctl_real, NULL, &eeprom_contents },
100  { EEPROM_SYSCTL2, "data16-31", NULL, 0, 0644, NULL, &sensors_proc_real,
101    &sensors_sysctl_real, NULL, &eeprom_contents },
102  { EEPROM_SYSCTL3, "data32-47", NULL, 0, 0644, NULL, &sensors_proc_real,
103    &sensors_sysctl_real, NULL, &eeprom_contents },
104  { EEPROM_SYSCTL4, "data48-63", NULL, 0, 0644, NULL, &sensors_proc_real,
105    &sensors_sysctl_real, NULL, &eeprom_contents },
106  { EEPROM_SYSCTL5, "data64-79", NULL, 0, 0644, NULL, &sensors_proc_real,
107    &sensors_sysctl_real, NULL, &eeprom_contents },
108  { EEPROM_SYSCTL6, "data80-95", NULL, 0, 0644, NULL, &sensors_proc_real,
109    &sensors_sysctl_real, NULL, &eeprom_contents },
110  { EEPROM_SYSCTL7, "data96-111", NULL, 0, 0644, NULL, &sensors_proc_real,
111    &sensors_sysctl_real, NULL, &eeprom_contents },
112  { EEPROM_SYSCTL8, "data112-127", NULL, 0, 0644, NULL, &sensors_proc_real,
113    &sensors_sysctl_real, NULL, &eeprom_contents },
114  { 0 }
115};
116
117/* Used by init/cleanup */
118static int eeprom_initialized = 0;
119
120/* I choose here for semi-static LM78 allocation. Complete dynamic
121   allocation could also be used; the code needed for this would probably
122   take more memory than the datastructure takes now. */
123#define MAX_EEPROM_NR 8
124static struct i2c_client *eeprom_list[MAX_EEPROM_NR];
125
126
127int eeprom_attach_adapter(struct i2c_adapter *adapter)
128{
129  int address,err,i;
130  struct i2c_client *new_client;
131  struct eeprom_data *data;
132
133  err = 0;
134
135  /* OK, this is no detection. I know. It will do for now, though.  */
136
137  /* Set err only if a global error would make registering other clients
138     impossible too (like out-of-memory). */
139     
140  /* Serial EEPROMs for SMBus use addresses from 0x50 to 0x57 */
141  for (address = 0x50; (! err) && (address <= 0x57); address ++) {
142
143    /* Later on, we will keep a list of registered addresses for each
144       adapter, and check whether they are used here */
145   
146    if (smbus_read_byte(adapter,address) < 0) {
147#ifdef DEBUG
148      printk("eeprom.o: No eeprom found at: 0x%X\n",address);
149#endif
150      continue;
151    }
152
153    /* Real detection code goes here */
154
155    /* Allocate space for a new client structure */
156    if (! (new_client =  kmalloc(sizeof(struct i2c_client) +
157                                sizeof(struct eeprom_data),
158                               GFP_KERNEL))) {
159      err = -ENOMEM;
160      continue;
161    }
162
163    /* Find a place in our global list */
164    for (i = 0; i < MAX_EEPROM_NR; i++)
165      if (! eeprom_list[i])
166         break;
167    if (i == MAX_EEPROM_NR) {
168      err = -ENOMEM;
169      printk("eeprom.o: No empty slots left, recompile and heighten "
170             "MAX_EEPROM_NR!\n");
171      goto ERROR1;
172    }
173    eeprom_list[i] = new_client;
174   
175    /* Fill the new client structure with data */
176    data = (struct eeprom_data *) (new_client + 1);
177    new_client->data = data;
178    new_client->id = i;
179    new_client->addr = address;
180    new_client->adapter = adapter;
181    new_client->driver = &eeprom_driver;
182    strcpy(new_client->name,"EEPROM chip");
183    data->valid = 0;
184    data->update_lock = MUTEX;
185   
186    /* Tell i2c-core a new client has arrived */
187    if ((err = i2c_attach_client(new_client)))
188      goto ERROR2;
189   
190    /* Register a new directory entry with module sensors */
191    if ((err = sensors_register_entry(new_client,"eeprom",
192                                      eeprom_dir_table_template)) < 0)
193      goto ERROR3;
194    data->sysctl_id = err;
195    err = 0;
196
197    continue;
198/* OK, this is not exactly good programming practice, usually. But it is
199   very code-efficient in this case. */
200
201ERROR3:
202    i2c_detach_client(new_client);
203ERROR2:
204    eeprom_list[i] = NULL;
205ERROR1:
206    kfree(new_client);
207  }
208  return err;
209}
210
211int eeprom_detach_client(struct i2c_client *client)
212{
213  int err,i;
214  for (i = 0; i < MAX_EEPROM_NR; i++)
215    if (client == eeprom_list[i])
216      break;
217  if ((i == MAX_EEPROM_NR)) {
218    printk("eeprom.o: Client to detach not found.\n");
219    return -ENOENT;
220  }
221
222  sensors_deregister_entry(((struct eeprom_data *)(client->data))->sysctl_id);
223
224  if ((err = i2c_detach_client(client))) {
225    printk("eeprom.o: Client deregistration failed, client not detached.\n");
226    return err;
227  }
228
229  eeprom_list[i] = NULL;
230  kfree(client);
231  return 0;
232}
233
234
235/* No commands defined yet */
236int eeprom_command(struct i2c_client *client, unsigned int cmd, void *arg)
237{
238  return 0;
239}
240
241void eeprom_inc_use (struct i2c_client *client)
242{
243#ifdef MODULE
244  MOD_INC_USE_COUNT;
245#endif
246}
247
248void eeprom_dec_use (struct i2c_client *client)
249{
250#ifdef MODULE
251  MOD_DEC_USE_COUNT;
252#endif
253}
254
255u16 swap_bytes(u16 val)
256{
257  return (val >> 8) | (val << 8);
258}
259
260/* No swapping needed here! */
261int eeprom_write_value(struct i2c_client *client, u8 reg, u16 value)
262{/*
263  if (reg == EEPROM_REG_CONF)
264    return smbus_write_byte_data(client->adapter,client->addr,reg,value);
265  else
266    return smbus_write_word_data(client->adapter,client->addr,reg,value); */
267   
268    /* No writes yet (PAE) */
269    return 0;
270}
271
272void eeprom_update_client(struct i2c_client *client)
273{
274  struct eeprom_data *data = client->data;
275  int i;
276
277  down(&data->update_lock);
278
279  if ((jiffies - data->last_updated > HZ+HZ/2 ) ||
280      (jiffies < data->last_updated) || ! data->valid) {
281
282#ifdef DEBUG
283    printk("Starting eeprom update\n");
284#endif
285
286   if (smbus_write_byte(client->adapter,client->addr,0)) {
287#ifdef DEBUG
288    printk("eeprom read start has failed!\n");
289#endif         
290   }
291    for (i=0;i<EEPROM_SIZE;i++) {
292         data->data[i] = (u8)smbus_read_byte(client->adapter,client->addr);
293    }
294   
295    data->last_updated = jiffies;
296    data->valid = 1;
297  }
298
299  up(&data->update_lock);
300}
301
302
303void eeprom_contents(struct i2c_client *client, int operation, int ctl_name,
304               int *nrels_mag, long *results)
305{
306  int i;
307  int base=0;
308  struct eeprom_data *data = client->data;
309 
310  if (ctl_name == EEPROM_SYSCTL2){ base=16; }
311  if (ctl_name == EEPROM_SYSCTL3){ base=32; }
312  if (ctl_name == EEPROM_SYSCTL4){ base=48; }
313  if (ctl_name == EEPROM_SYSCTL5){ base=64; }
314  if (ctl_name == EEPROM_SYSCTL6){ base=80; }
315  if (ctl_name == EEPROM_SYSCTL7){ base=96; }
316  if (ctl_name == EEPROM_SYSCTL8){ base=112; }
317 
318  if (operation == SENSORS_PROC_REAL_INFO)
319    *nrels_mag = 0;
320  else if (operation == SENSORS_PROC_REAL_READ) {
321    eeprom_update_client(client);
322    for (i=0; i<16; i++) {
323        results[i]=data->data[i + base];
324    }
325#ifdef DEBUG
326    printk("eeprom.o: 0x%X EEPROM Contents (base %d): ",client->addr,base);
327    for (i=0; i<16; i++) {
328      printk(" 0x%X",data->data[i + base]);
329    }
330    printk(" .\n");
331#endif
332    *nrels_mag = 16;
333  } else if (operation == SENSORS_PROC_REAL_WRITE) {
334
335/* No writes to the EEPROM (yet, anyway) (PAE) */
336        printk("eeprom.o: No writes to EEPROMs supported!\n");
337  }
338}
339
340int eeprom_init(void)
341{
342  int res;
343
344  printk("eeprom.o version %s (%s)\n",LM_VERSION,LM_DATE);
345  eeprom_initialized = 0;
346  if ((res = i2c_add_driver(&eeprom_driver))) {
347    printk("eeprom.o: Driver registration failed, module not inserted.\n");
348    eeprom_cleanup();
349    return res;
350  }
351  eeprom_initialized ++;
352  return 0;
353}
354
355int eeprom_cleanup(void)
356{
357  int res;
358
359  if (eeprom_initialized >= 1) {
360    if ((res = i2c_del_driver(&eeprom_driver))) {
361      printk("eeprom.o: Driver deregistration failed, module not removed.\n");
362      return res;
363    }
364  } else
365    eeprom_initialized --;
366
367  return 0;
368}
369
370
371#ifdef MODULE
372
373MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and Philip Edelbrock <phil@netroedge.com>");
374MODULE_DESCRIPTION("EEPROM driver");
375
376int init_module(void)
377{
378  return eeprom_init();
379}
380
381int cleanup_module(void)
382{
383  return eeprom_cleanup();
384}
385
386#endif /* MODULE */
387
Note: See TracBrowser for help on using the browser.