| 1 | /* |
|---|
| 2 | matorb.c - Part of lm_sensors, Linux kernel modules for hardware |
|---|
| 3 | monitoring |
|---|
| 4 | Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> |
|---|
| 5 | and 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 | |
|---|
| 23 | #define DEBUG 1 |
|---|
| 24 | |
|---|
| 25 | #include <linux/module.h> |
|---|
| 26 | #include <linux/slab.h> |
|---|
| 27 | #include <linux/i2c.h> |
|---|
| 28 | #include <linux/i2c-proc.h> |
|---|
| 29 | #include <linux/init.h> |
|---|
| 30 | #include "version.h" |
|---|
| 31 | |
|---|
| 32 | MODULE_LICENSE("GPL"); |
|---|
| 33 | |
|---|
| 34 | /* Addresses to scan */ |
|---|
| 35 | static unsigned short normal_i2c[] = { 0x2E, SENSORS_I2C_END }; |
|---|
| 36 | static unsigned short normal_i2c_range[] = { SENSORS_I2C_END }; |
|---|
| 37 | static unsigned int normal_isa[] = { SENSORS_ISA_END }; |
|---|
| 38 | static unsigned int normal_isa_range[] = { SENSORS_ISA_END }; |
|---|
| 39 | |
|---|
| 40 | /* Insmod parameters */ |
|---|
| 41 | SENSORS_INSMOD_1(matorb); |
|---|
| 42 | |
|---|
| 43 | /* Many MATORB constants specified below */ |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | /* Each client has this additional data */ |
|---|
| 47 | struct matorb_data { |
|---|
| 48 | struct i2c_client client; |
|---|
| 49 | int sysctl_id; |
|---|
| 50 | |
|---|
| 51 | struct semaphore update_lock; |
|---|
| 52 | char valid; /* !=0 if following fields are valid */ |
|---|
| 53 | unsigned long last_updated; /* In jiffies */ |
|---|
| 54 | |
|---|
| 55 | }; |
|---|
| 56 | |
|---|
| 57 | static int matorb_attach_adapter(struct i2c_adapter *adapter); |
|---|
| 58 | static int matorb_detect(struct i2c_adapter *adapter, int address, |
|---|
| 59 | unsigned short flags, int kind); |
|---|
| 60 | static void matorb_init_client(struct i2c_client *client); |
|---|
| 61 | static int matorb_detach_client(struct i2c_client *client); |
|---|
| 62 | |
|---|
| 63 | static int matorb_write_value(struct i2c_client *client, u8 reg, |
|---|
| 64 | u16 value); |
|---|
| 65 | static void matorb_disp(struct i2c_client *client, int operation, |
|---|
| 66 | int ctl_name, int *nrels_mag, long *results); |
|---|
| 67 | static void matorb_update_client(struct i2c_client *client); |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | /* This is the driver that will be inserted */ |
|---|
| 71 | static struct i2c_driver matorb_driver = { |
|---|
| 72 | .name = "Matrix Orbital LCD driver", |
|---|
| 73 | .id = I2C_DRIVERID_MATORB, |
|---|
| 74 | .flags = I2C_DF_NOTIFY, |
|---|
| 75 | .attach_adapter = matorb_attach_adapter, |
|---|
| 76 | .detach_client = matorb_detach_client, |
|---|
| 77 | }; |
|---|
| 78 | |
|---|
| 79 | /* -- SENSORS SYSCTL START -- */ |
|---|
| 80 | #define MATORB_SYSCTL_DISP 1000 |
|---|
| 81 | /* -- SENSORS SYSCTL END -- */ |
|---|
| 82 | |
|---|
| 83 | /* These files are created for each detected MATORB. This is just a template; |
|---|
| 84 | though at first sight, you might think we could use a statically |
|---|
| 85 | allocated list, we need some way to get back to the parent - which |
|---|
| 86 | is done through one of the 'extra' fields which are initialized |
|---|
| 87 | when a new copy is allocated. */ |
|---|
| 88 | static ctl_table matorb_dir_table_template[] = { |
|---|
| 89 | {MATORB_SYSCTL_DISP, "disp", NULL, 0, 0644, NULL, &i2c_proc_real, |
|---|
| 90 | &i2c_sysctl_real, NULL, &matorb_disp}, |
|---|
| 91 | {0} |
|---|
| 92 | }; |
|---|
| 93 | |
|---|
| 94 | static int matorb_id = 0; |
|---|
| 95 | |
|---|
| 96 | static int matorb_attach_adapter(struct i2c_adapter *adapter) |
|---|
| 97 | { |
|---|
| 98 | return i2c_detect(adapter, &addr_data, matorb_detect); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | /* This function is called by i2c_detect */ |
|---|
| 102 | int matorb_detect(struct i2c_adapter *adapter, int address, |
|---|
| 103 | unsigned short flags, int kind) |
|---|
| 104 | { |
|---|
| 105 | int i, cur; |
|---|
| 106 | struct i2c_client *new_client; |
|---|
| 107 | struct matorb_data *data; |
|---|
| 108 | int err = 0; |
|---|
| 109 | const char *type_name = "matorb"; |
|---|
| 110 | const char *client_name = "matorb"; |
|---|
| 111 | |
|---|
| 112 | /* Make sure we aren't probing the ISA bus!! This is just a safety check |
|---|
| 113 | at this moment; i2c_detect really won't call us. */ |
|---|
| 114 | #ifdef DEBUG |
|---|
| 115 | if (i2c_is_isa_adapter(adapter)) { |
|---|
| 116 | printk |
|---|
| 117 | ("matorb.o: matorb_detect called for an ISA bus adapter?!?\n"); |
|---|
| 118 | return 0; |
|---|
| 119 | } |
|---|
| 120 | #endif |
|---|
| 121 | |
|---|
| 122 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WRITE_BYTE | |
|---|
| 123 | I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) |
|---|
| 124 | goto ERROR0; |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | /* OK. For now, we presume we have a valid client. We now create the |
|---|
| 128 | client structure, even though we cannot fill it completely yet. |
|---|
| 129 | But it allows us to access matorb_{read,write}_value. */ |
|---|
| 130 | if (!(data = kmalloc(sizeof(struct matorb_data), GFP_KERNEL))) { |
|---|
| 131 | err = -ENOMEM; |
|---|
| 132 | goto ERROR0; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | new_client = &data->client; |
|---|
| 136 | new_client->addr = address; |
|---|
| 137 | new_client->data = data; |
|---|
| 138 | new_client->adapter = adapter; |
|---|
| 139 | new_client->driver = &matorb_driver; |
|---|
| 140 | new_client->flags = 0; |
|---|
| 141 | |
|---|
| 142 | /* Now, we do the remaining detection. It is lousy. */ |
|---|
| 143 | cur = i2c_smbus_write_byte_data(new_client, 0x0FE, 0x58); /* clear screen */ |
|---|
| 144 | |
|---|
| 145 | printk("matorb.o: debug detect 0x%X\n", cur); |
|---|
| 146 | |
|---|
| 147 | /* Fill in the remaining client fields and put it into the global list */ |
|---|
| 148 | strcpy(new_client->name, client_name); |
|---|
| 149 | |
|---|
| 150 | new_client->id = matorb_id++; |
|---|
| 151 | data->valid = 0; |
|---|
| 152 | init_MUTEX(&data->update_lock); |
|---|
| 153 | |
|---|
| 154 | /* Tell the I2C layer a new client has arrived */ |
|---|
| 155 | if ((err = i2c_attach_client(new_client))) |
|---|
| 156 | goto ERROR3; |
|---|
| 157 | |
|---|
| 158 | /* Register a new directory entry with module sensors */ |
|---|
| 159 | if ((i = i2c_register_entry(new_client, type_name, |
|---|
| 160 | matorb_dir_table_template)) < 0) { |
|---|
| 161 | err = i; |
|---|
| 162 | goto ERROR4; |
|---|
| 163 | } |
|---|
| 164 | data->sysctl_id = i; |
|---|
| 165 | |
|---|
| 166 | matorb_init_client(new_client); |
|---|
| 167 | return 0; |
|---|
| 168 | |
|---|
| 169 | /* OK, this is not exactly good programming practice, usually. But it is |
|---|
| 170 | very code-efficient in this case. */ |
|---|
| 171 | |
|---|
| 172 | ERROR4: |
|---|
| 173 | i2c_detach_client(new_client); |
|---|
| 174 | ERROR3: |
|---|
| 175 | kfree(data); |
|---|
| 176 | ERROR0: |
|---|
| 177 | return err; |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | static int matorb_detach_client(struct i2c_client *client) |
|---|
| 181 | { |
|---|
| 182 | int err; |
|---|
| 183 | |
|---|
| 184 | i2c_deregister_entry(((struct matorb_data *) (client->data))-> |
|---|
| 185 | sysctl_id); |
|---|
| 186 | |
|---|
| 187 | if ((err = i2c_detach_client(client))) { |
|---|
| 188 | printk |
|---|
| 189 | ("matorb.o: Client deregistration failed, client not detached.\n"); |
|---|
| 190 | return err; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | kfree(client->data); |
|---|
| 194 | |
|---|
| 195 | return 0; |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | #if 0 |
|---|
| 200 | /* All registers are word-sized, except for the configuration register. |
|---|
| 201 | MATORB uses a high-byte first convention, which is exactly opposite to |
|---|
| 202 | the usual practice. */ |
|---|
| 203 | static int matorb_read_value(struct i2c_client *client, u8 reg) |
|---|
| 204 | { |
|---|
| 205 | return -1; /* Doesn't support reads */ |
|---|
| 206 | } |
|---|
| 207 | #endif |
|---|
| 208 | |
|---|
| 209 | /* All registers are word-sized, except for the configuration register. |
|---|
| 210 | MATORB uses a high-byte first convention, which is exactly opposite to |
|---|
| 211 | the usual practice. */ |
|---|
| 212 | static int matorb_write_value(struct i2c_client *client, u8 reg, u16 value) |
|---|
| 213 | { |
|---|
| 214 | if (reg == 0) { |
|---|
| 215 | return i2c_smbus_write_byte(client, value); |
|---|
| 216 | } else { |
|---|
| 217 | return i2c_smbus_write_byte_data(client, reg, value); |
|---|
| 218 | } |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | static void matorb_init_client(struct i2c_client *client) |
|---|
| 222 | { |
|---|
| 223 | /* Initialize the MATORB chip */ |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | static void matorb_update_client(struct i2c_client *client) |
|---|
| 227 | { |
|---|
| 228 | struct matorb_data *data = client->data; |
|---|
| 229 | |
|---|
| 230 | down(&data->update_lock); |
|---|
| 231 | |
|---|
| 232 | if ((jiffies - data->last_updated > HZ + HZ / 2) || |
|---|
| 233 | (jiffies < data->last_updated) || !data->valid) { |
|---|
| 234 | |
|---|
| 235 | #ifdef DEBUG |
|---|
| 236 | printk("Starting matorb update\n"); |
|---|
| 237 | #endif |
|---|
| 238 | |
|---|
| 239 | /* nothing yet */ |
|---|
| 240 | data->last_updated = jiffies; |
|---|
| 241 | data->valid = 1; |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | up(&data->update_lock); |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | void matorb_disp(struct i2c_client *client, int operation, int ctl_name, |
|---|
| 249 | int *nrels_mag, long *results) |
|---|
| 250 | { |
|---|
| 251 | int i; |
|---|
| 252 | |
|---|
| 253 | if (operation == SENSORS_PROC_REAL_INFO) |
|---|
| 254 | *nrels_mag = 0; |
|---|
| 255 | else if (operation == SENSORS_PROC_REAL_READ) { |
|---|
| 256 | matorb_update_client(client); |
|---|
| 257 | results[0] = 0; |
|---|
| 258 | *nrels_mag = 3; |
|---|
| 259 | } else if (operation == SENSORS_PROC_REAL_WRITE) { |
|---|
| 260 | for (i = 1; i <= *nrels_mag; i++) { |
|---|
| 261 | matorb_write_value(client, 0, results[i - 1]); |
|---|
| 262 | } |
|---|
| 263 | } |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | static int __init sm_matorb_init(void) |
|---|
| 267 | { |
|---|
| 268 | printk("matorb.o version %s (%s)\n", LM_VERSION, LM_DATE); |
|---|
| 269 | return i2c_add_driver(&matorb_driver); |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | static void __exit sm_matorb_exit(void) |
|---|
| 273 | { |
|---|
| 274 | i2c_del_driver(&matorb_driver); |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | MODULE_AUTHOR |
|---|
| 280 | ("Frodo Looijaard <frodol@dds.nl> and Philip Edelbrock <phil@netroedge.com>"); |
|---|
| 281 | MODULE_DESCRIPTION("MATORB driver"); |
|---|
| 282 | |
|---|
| 283 | module_init(sm_matorb_init); |
|---|
| 284 | module_exit(sm_matorb_exit); |
|---|