root/lm-sensors/trunk/kernel/chips/icspll.c @ 499

Revision 499, 9.8 KB (checked in by kmalkki, 15 years ago)

(Kyösti)

  • renamed every smbus_* access to i2c_smbus_*
  • everything in lm_sensors2/kernel/* compiles nicely :)
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2    icspll.c - Part of lm_sensors, Linux kernel modules for hardware
3               monitoring
4    Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>,
5    Philip Edelbrock <phil@netroedge.com>,
6    and Mark Studebaker <mds@eng.paradyne.com>
7
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
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21*/
22
23/*
24    ** WARNING  **
25    Supports limited combinations of clock chips and busses.
26    Use on unsupported chips may crash your system.
27    See doc/chips/icspll for details.
28*/
29
30
31#include <linux/module.h>
32#include <linux/malloc.h>
33#include <linux/i2c.h>
34#include "sensors.h"
35#include "i2c-isa.h"
36#include "version.h"
37
38/* Many constants specified below */
39
40#define ICSPLL_SIZE 7
41#define MAXBLOCK_SIZE 32
42
43/* Each client has this additional data */
44struct icspll_data {
45         int sysctl_id;
46
47         struct semaphore update_lock;
48         char valid;                 /* !=0 if following fields are valid */
49         unsigned long last_updated; /* In jiffies */
50
51         u8 data[ICSPLL_SIZE];     /* Register values */
52         int memtype;
53};
54
55#ifdef MODULE
56extern int init_module(void);
57extern int cleanup_module(void);
58#endif /* MODULE */
59
60static int icspll_init(void);
61static int icspll_cleanup(void);
62
63static int icspll_attach_adapter(struct i2c_adapter *adapter);
64static int icspll_detach_client(struct i2c_client *client);
65static int icspll_command(struct i2c_client *client, unsigned int cmd,
66                        void *arg);
67                       
68static void icspll_inc_use (struct i2c_client *client);
69static void icspll_dec_use (struct i2c_client *client);
70
71#if 0
72static int icspll_write_value(struct i2c_client *client, u8 reg, u16 value);
73#endif
74
75static void icspll_contents(struct i2c_client *client, int operation, int ctl_name,
76                      int *nrels_mag, long *results);
77static void icspll_update_client(struct i2c_client *client);
78
79
80/* This is the driver that will be inserted */
81static struct i2c_driver icspll_driver = {
82  /* name */            "ICS/PLL clock reader",
83  /* id */              I2C_DRIVERID_ICSPLL,
84  /* flags */           I2C_DF_NOTIFY,
85  /* attach_adapter */  &icspll_attach_adapter,
86  /* detach_client */   &icspll_detach_client,
87  /* command */         &icspll_command,
88  /* inc_use */         &icspll_inc_use,
89  /* dec_use */         &icspll_dec_use
90};
91
92/* These files are created for each detected ICSPLL. 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 icspll_dir_table_template[] = {
98  { ICSPLL_SYSCTL1, "reg0-6", NULL, 0, 0444, NULL, &sensors_proc_real,
99    &sensors_sysctl_real, NULL, &icspll_contents },
100  { 0 }
101};
102
103/* holding place for data - block read could be as much as 32 */
104static u8 tempdata[MAXBLOCK_SIZE];
105
106/* Used by init/cleanup */
107static int icspll_initialized = 0;
108
109/* I choose here for semi-static allocation. Complete dynamic
110   allocation could also be used; the code needed for this would probably
111   take more memory than the datastructure takes now. */
112#define MAX_ICSPLL_NR 2
113static struct i2c_client *icspll_list[MAX_ICSPLL_NR];
114
115
116int icspll_attach_adapter(struct i2c_adapter *adapter)
117{
118  int address,err,i;
119  struct i2c_client *new_client;
120  struct icspll_data *data;
121
122  err = 0;
123  /* Make sure we aren't probing the ISA bus!! */
124  if (i2c_is_isa_adapter(adapter)) return 0;
125 
126  /* OK, this is no detection. I know. It will do for now, though.  */
127
128  /* Set err only if a global error would make registering other clients
129     impossible too (like out-of-memory). */
130     
131  /* ICS and PLL clock chips seem to all be at 0x69.
132     Don't know what is out there at 0x6A, don't scan that yet. */
133
134  for (address = 0x69; (! err) && (address <= 0x69); address ++) {
135
136    /* Later on, we will keep a list of registered addresses for each
137       adapter, and check whether they are used here */
138   
139    /* these chips only support block transfers so use that for detection */
140    if(i2c_smbus_read_block_data(adapter, address, 0x00, tempdata) < 0) {
141#ifdef DEBUG
142      printk("icspll.o: No icspll found at: 0x%X\n",address);
143#endif
144      continue;
145    }
146
147    /* Real detection code goes here */
148
149    /* Allocate space for a new client structure */
150    if (! (new_client =  kmalloc(sizeof(struct i2c_client) +
151                                sizeof(struct icspll_data),
152                               GFP_KERNEL))) {
153      err = -ENOMEM;
154      continue;
155    }
156
157    /* Find a place in our global list */
158    for (i = 0; i < MAX_ICSPLL_NR; i++)
159      if (! icspll_list[i])
160         break;
161    if (i == MAX_ICSPLL_NR) {
162      err = -ENOMEM;
163      printk("icspll.o: No empty slots left, recompile and heighten "
164             "MAX_ICSPLL_NR!\n");
165      goto ERROR1;
166    }
167    icspll_list[i] = new_client;
168   
169    /* Fill the new client structure with data */
170    data = (struct icspll_data *) (new_client + 1);
171    new_client->data = data;
172    new_client->id = i;
173    new_client->addr = address;
174    new_client->adapter = adapter;
175    new_client->driver = &icspll_driver;
176    strcpy(new_client->name,"ICSPLL chip");
177    data->valid = 0;
178    init_MUTEX(&data->update_lock);
179/* fill data structure so unknown registers are 0xFF */
180    data->data[0] = ICSPLL_SIZE;
181    for(i = 1; i <= ICSPLL_SIZE; i++)
182      data->data[i] = 0xFF;
183   
184    /* Tell i2c-core a new client has arrived */
185    if ((err = i2c_attach_client(new_client)))
186      goto ERROR2;
187   
188    /* Register a new directory entry with module sensors */
189    if ((err = sensors_register_entry(new_client,"icspll",
190                                      icspll_dir_table_template)) < 0)
191      goto ERROR3;
192    data->sysctl_id = err;
193    err = 0;
194
195    continue;
196/* OK, this is not exactly good programming practice, usually. But it is
197   very code-efficient in this case. */
198
199ERROR3:
200    i2c_detach_client(new_client);
201ERROR2:
202    icspll_list[i] = NULL;
203ERROR1:
204    kfree(new_client);
205  }
206  return err;
207}
208
209int icspll_detach_client(struct i2c_client *client)
210{
211  int err,i;
212  for (i = 0; i < MAX_ICSPLL_NR; i++)
213    if (client == icspll_list[i])
214      break;
215  if ((i == MAX_ICSPLL_NR)) {
216    printk("icspll.o: Client to detach not found.\n");
217    return -ENOENT;
218  }
219
220  sensors_deregister_entry(((struct icspll_data *)(client->data))->sysctl_id);
221
222  if ((err = i2c_detach_client(client))) {
223    printk("icspll.o: Client deregistration failed, client not detached.\n");
224    return err;
225  }
226
227  icspll_list[i] = NULL;
228  kfree(client);
229  return 0;
230}
231
232
233/* No commands defined yet */
234int icspll_command(struct i2c_client *client, unsigned int cmd, void *arg)
235{
236  return 0;
237}
238
239void icspll_inc_use (struct i2c_client *client)
240{
241#ifdef MODULE
242  MOD_INC_USE_COUNT;
243#endif
244}
245
246void icspll_dec_use (struct i2c_client *client)
247{
248#ifdef MODULE
249  MOD_DEC_USE_COUNT;
250#endif
251}
252
253#if 0
254/* No writes yet (PAE) */
255int icspll_write_value(struct i2c_client *client, u8 reg, u16 value)
256{
257  return i2c_smbus_write_block_data(client->adapter,client->addr,reg,value);
258}
259#endif
260
261void icspll_update_client(struct i2c_client *client)
262{
263  struct icspll_data *data = client->data;
264  int i, len;
265
266  down(&data->update_lock);
267
268  if ((jiffies - data->last_updated > HZ+HZ/2 ) ||
269      (jiffies < data->last_updated) || ! data->valid) {
270
271    len = i2c_smbus_read_block_data(client->adapter, client->addr, 0x00, tempdata);
272#ifdef DEBUG
273    printk("icspll.o: read returned %d values\n", len);
274#endif
275    if(len > ICSPLL_SIZE)
276      len = ICSPLL_SIZE;
277    for(i = 0; i < len; i++)
278      data->data[i] = tempdata[i];
279   
280    data->last_updated = jiffies;
281    data->valid = 1;
282  }
283
284  up(&data->update_lock);
285}
286
287
288void icspll_contents(struct i2c_client *client, int operation, int ctl_name,
289               int *nrels_mag, long *results)
290{
291  int i;
292  struct icspll_data *data = client->data;
293 
294  if (operation == SENSORS_PROC_REAL_INFO)
295    *nrels_mag = 0;
296  else if (operation == SENSORS_PROC_REAL_READ) {
297    icspll_update_client(client);
298    for (i=0; i< ICSPLL_SIZE; i++) {
299        results[i]=data->data[i];
300    }
301#ifdef DEBUG
302    printk("icspll.o: 0x%X ICSPLL Contents: ",client->addr);
303    for (i=0; i< ICSPLL_SIZE; i++) {
304      printk(" 0x%X",data->data[i]);
305    }
306    printk(" .\n");
307#endif
308    *nrels_mag = ICSPLL_SIZE;
309  } else if (operation == SENSORS_PROC_REAL_WRITE) {
310
311/* No writes to the ICSPLL (yet, anyway) (PAE) */
312    printk("icspll.o: No writes to ICSPLLs supported!\n");
313  }
314}
315
316int icspll_init(void)
317{
318  int res;
319
320  printk("icspll.o version %s (%s)\n",LM_VERSION,LM_DATE);
321  icspll_initialized = 0;
322  if ((res = i2c_add_driver(&icspll_driver))) {
323    printk("icspll.o: Driver registration failed, module not inserted.\n");
324    icspll_cleanup();
325    return res;
326  }
327  icspll_initialized ++;
328  return 0;
329}
330
331int icspll_cleanup(void)
332{
333  int res;
334
335  if (icspll_initialized >= 1) {
336    if ((res = i2c_del_driver(&icspll_driver))) {
337      printk("icspll.o: Driver deregistration failed, module not removed.\n");
338      return res;
339    }
340  } else
341    icspll_initialized --;
342
343  return 0;
344}
345
346
347#ifdef MODULE
348
349MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, Philip Edelbrock <phil@netroedge.com>, and Mark Studebaker <mds@eng.paradyne.com>");
350MODULE_DESCRIPTION("ICSPLL driver");
351
352int init_module(void)
353{
354  return icspll_init();
355}
356
357int cleanup_module(void)
358{
359  return icspll_cleanup();
360}
361
362#endif /* MODULE */
363
Note: See TracBrowser for help on using the browser.