| 1 | /* $Id: drv_generic_i2c.c,v 1.5 2005/06/01 12:50:25 reinelt Exp $ |
|---|
| 2 | * |
|---|
| 3 | * generic driver helper for i2c displays |
|---|
| 4 | * |
|---|
| 5 | * Copyright (C) 2005 Luis Correia <lfcorreia@users.sf.net> |
|---|
| 6 | * Copyright (C) 2005 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net> |
|---|
| 7 | * |
|---|
| 8 | * This file is part of LCD4Linux. |
|---|
| 9 | * |
|---|
| 10 | * LCD4Linux is free software; you can redistribute it and/or modify |
|---|
| 11 | * it under the terms of the GNU General Public License as published by |
|---|
| 12 | * the Free Software Foundation; either version 2, or (at your option) |
|---|
| 13 | * any later version. |
|---|
| 14 | * |
|---|
| 15 | * LCD4Linux is distributed in the hope that it will be useful, |
|---|
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 18 | * GNU General Public License for more details. |
|---|
| 19 | * |
|---|
| 20 | * You should have received a copy of the GNU General Public License |
|---|
| 21 | * along with this program; if not, write to the Free Software |
|---|
| 22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|---|
| 23 | * |
|---|
| 24 | * |
|---|
| 25 | * $Log: drv_generic_i2c.c,v $ |
|---|
| 26 | * Revision 1.5 2005/06/01 12:50:25 reinelt |
|---|
| 27 | * ifdef'ed unused function to avoid compiler warning |
|---|
| 28 | * |
|---|
| 29 | * Revision 1.4 2005/05/31 20:42:55 lfcorreia |
|---|
| 30 | * new file: lcd4linux_i2c.h |
|---|
| 31 | * avoid the problems detecting the proper I2C kernel include files |
|---|
| 32 | * |
|---|
| 33 | * rearrange all the other autoconf stuff to remove I2C detection |
|---|
| 34 | * |
|---|
| 35 | * new method by Paul Kamphuis to write to the I2C device |
|---|
| 36 | * |
|---|
| 37 | * Revision 1.3 2005/05/08 04:32:44 reinelt |
|---|
| 38 | * CodingStyle added and applied |
|---|
| 39 | * |
|---|
| 40 | * Revision 1.2 2005/04/05 05:12:57 reinelt |
|---|
| 41 | * i2c patch from Paul (still does not work here :-( |
|---|
| 42 | * |
|---|
| 43 | * Revision 1.1 2005/03/28 19:39:23 reinelt |
|---|
| 44 | * HD44780/I2C patch from Luis merged (still does not work for me) |
|---|
| 45 | * |
|---|
| 46 | * |
|---|
| 47 | */ |
|---|
| 48 | |
|---|
| 49 | /* |
|---|
| 50 | DISCLAIMER!!!! |
|---|
| 51 | |
|---|
| 52 | The following code is WORK IN PROGRESS, since it basicly 'works for us...' |
|---|
| 53 | |
|---|
| 54 | (C) 2005 Paul Kamphuis & Luis Correia |
|---|
| 55 | |
|---|
| 56 | We have removed all of the delays from this code, as the I2C bus is slow enough... |
|---|
| 57 | (maximum possible speed is 100KHz only) |
|---|
| 58 | |
|---|
| 59 | */ |
|---|
| 60 | |
|---|
| 61 | #include "config.h" |
|---|
| 62 | |
|---|
| 63 | #include <stdlib.h> |
|---|
| 64 | #include <stdio.h> |
|---|
| 65 | #include <string.h> |
|---|
| 66 | #include <errno.h> |
|---|
| 67 | #include <unistd.h> |
|---|
| 68 | #include <termios.h> |
|---|
| 69 | #include <fcntl.h> |
|---|
| 70 | #include <time.h> |
|---|
| 71 | #include <signal.h> |
|---|
| 72 | #include <sys/types.h> |
|---|
| 73 | #include <sys/stat.h> |
|---|
| 74 | #include <sys/ioctl.h> |
|---|
| 75 | |
|---|
| 76 | #include "lcd4linux_i2c.h" |
|---|
| 77 | #include "debug.h" |
|---|
| 78 | #include "qprintf.h" |
|---|
| 79 | #include "cfg.h" |
|---|
| 80 | #include "udelay.h" |
|---|
| 81 | #include "drv_generic_i2c.h" |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | static char *Driver = ""; |
|---|
| 85 | static char *Section = ""; |
|---|
| 86 | static int i2c_device; |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | static void my_i2c_smbus_write_byte_data(const int device, const unsigned char val) |
|---|
| 90 | { |
|---|
| 91 | struct i2c_smbus_ioctl_data args; |
|---|
| 92 | union i2c_smbus_data data; |
|---|
| 93 | args.read_write = I2C_SMBUS_WRITE; |
|---|
| 94 | args.command = val; |
|---|
| 95 | args.size = I2C_SMBUS_BYTE_DATA; |
|---|
| 96 | data.byte = val; |
|---|
| 97 | args.data = &data; |
|---|
| 98 | if (ioctl(device, I2C_SMBUS, &args) < 0) { |
|---|
| 99 | info("I2C: device %s IOCTL failed !\n", device); |
|---|
| 100 | } |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | /* unused, ifdef'ed away to avoid compiler warning */ |
|---|
| 104 | #if 0 |
|---|
| 105 | static void my_i2c_smbus_read_byte_data(const int device, const unsigned char data) |
|---|
| 106 | { |
|---|
| 107 | struct i2c_smbus_ioctl_data args; |
|---|
| 108 | args.read_write = I2C_SMBUS_READ; |
|---|
| 109 | args.command = data; |
|---|
| 110 | args.size = I2C_SMBUS_BYTE; |
|---|
| 111 | args.data = 0; |
|---|
| 112 | ioctl(device, I2C_SMBUS, &args); |
|---|
| 113 | } |
|---|
| 114 | #endif |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | int drv_generic_i2c_open(const char *section, const char *driver) |
|---|
| 118 | { |
|---|
| 119 | int dev; |
|---|
| 120 | char *bus, *device; |
|---|
| 121 | udelay_init(); |
|---|
| 122 | Section = (char *) section; |
|---|
| 123 | Driver = (char *) driver; |
|---|
| 124 | bus = cfg_get(Section, "Port", NULL); |
|---|
| 125 | device = cfg_get(Section, "Device", NULL); |
|---|
| 126 | dev = atoi(device); |
|---|
| 127 | info("%s: initializing I2C bus %s", Driver, bus); |
|---|
| 128 | info("device %d", dev); |
|---|
| 129 | if ((i2c_device = open(bus, O_WRONLY)) < 0) { |
|---|
| 130 | error("%s: I2C bus %s open failed !\n", Driver, bus); |
|---|
| 131 | return -1; |
|---|
| 132 | } |
|---|
| 133 | info("%s: initializing I2C slave device 0x%x", Driver, dev); |
|---|
| 134 | if (ioctl(i2c_device, I2C_SLAVE, dev) < 0) { |
|---|
| 135 | error("%s: error initializing device 0x%x\n", Driver, dev); |
|---|
| 136 | close(i2c_device); |
|---|
| 137 | return -1; |
|---|
| 138 | } |
|---|
| 139 | return 0; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | int drv_generic_i2c_close(void) |
|---|
| 144 | { |
|---|
| 145 | close(i2c_device); |
|---|
| 146 | return 0; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | unsigned char drv_generic_i2c_wire(const char *name, const char *deflt) |
|---|
| 151 | { |
|---|
| 152 | unsigned char w; |
|---|
| 153 | char wire[256]; |
|---|
| 154 | char *s; |
|---|
| 155 | qprintf(wire, sizeof(wire), "Wire.%s", name); |
|---|
| 156 | s = cfg_get(Section, wire, deflt); |
|---|
| 157 | if (strlen(s) == 3 && strncasecmp(s, "DB", 2) == 0 && s[2] >= '0' && s[2] <= '7') { |
|---|
| 158 | w = s[2] - '0'; |
|---|
| 159 | } else if (strcasecmp(s, "GND") == 0) { |
|---|
| 160 | w = 0; |
|---|
| 161 | } else { |
|---|
| 162 | error("%s: unknown signal <%s> for wire <%s>", Driver, s, name); |
|---|
| 163 | error("%s: should be DB0..7 or GND", Driver); |
|---|
| 164 | return 0xff; |
|---|
| 165 | } |
|---|
| 166 | free(s); |
|---|
| 167 | if (w == 0) { |
|---|
| 168 | info("%s: wiring: [DISPLAY:%s]<==>[i2c:GND]", Driver, name); |
|---|
| 169 | } else { |
|---|
| 170 | info("%s: wiring: [DISPLAY:%s]<==>[i2c:DB%d]", Driver, name, w); |
|---|
| 171 | } |
|---|
| 172 | w = 1 << w; |
|---|
| 173 | return w; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | void drv_generic_i2c_data(const unsigned char data) |
|---|
| 178 | { |
|---|
| 179 | my_i2c_smbus_write_byte_data(i2c_device, data); |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | void drv_generic_i2c_command(const unsigned char command, const unsigned char *data, const unsigned char length) |
|---|
| 184 | { |
|---|
| 185 | i2c_smbus_write_block_data(i2c_device, command, length, (unsigned char *) data); |
|---|
| 186 | } |
|---|