source: rtems/c/src/lib/libbsp/powerpc/mvme3100/i2c/i2c_init.c @ b599faa

4.104.114.95
Last change on this file since b599faa was b599faa, checked in by Till Straumann <strauman@…>, on 12/14/07 at 06:30:15
  • imported MVME3100 BSP (from SLAC repository)
  • Property mode set to 100644
File size: 5.2 KB
Line 
1/* $Id$ */
2
3/* Register i2c bus driver & devices */
4
5/*
6 * Authorship
7 * ----------
8 * This software ('mvme3100' RTEMS BSP) was created by
9 *
10 *     Till Straumann <strauman@slac.stanford.edu>, 2005-2007,
11 *         Stanford Linear Accelerator Center, Stanford University.
12 *
13 * Acknowledgement of sponsorship
14 * ------------------------------
15 * The 'mvme3100' BSP was produced by
16 *     the Stanford Linear Accelerator Center, Stanford University,
17 *         under Contract DE-AC03-76SFO0515 with the Department of Energy.
18 *
19 * Government disclaimer of liability
20 * ----------------------------------
21 * Neither the United States nor the United States Department of Energy,
22 * nor any of their employees, makes any warranty, express or implied, or
23 * assumes any legal liability or responsibility for the accuracy,
24 * completeness, or usefulness of any data, apparatus, product, or process
25 * disclosed, or represents that its use would not infringe privately owned
26 * rights.
27 *
28 * Stanford disclaimer of liability
29 * --------------------------------
30 * Stanford University makes no representations or warranties, express or
31 * implied, nor assumes any liability for the use of this software.
32 *
33 * Stanford disclaimer of copyright
34 * --------------------------------
35 * Stanford University, owner of the copyright, hereby disclaims its
36 * copyright and all other rights in this software.  Hence, anyone may
37 * freely use it for any purpose without restriction. 
38 *
39 * Maintenance of notices
40 * ----------------------
41 * In the interest of clarity regarding the origin and status of this
42 * SLAC software, this and all the preceding Stanford University notices
43 * are to remain affixed to any copy or derivative of this software made
44 * or distributed by the recipient and are to be affixed to any copy of
45 * software made or distributed by the recipient that contains a copy or
46 * derivative of this software.
47 *
48 * ------------------ SLAC Software Notices, Set 4 OTT.002a, 2004 FEB 03
49 */
50
51#include <rtems.h>
52#include <bsp.h>
53#include <rtems/bspIo.h>
54#include <rtems/libi2c.h>
55#include <libchip/i2c-2b-eeprom.h>
56#include <libchip/i2c-ds1621.h>
57#include <bsp/mpc8540_i2c_busdrv.h>
58#include <rtems/libio.h>
59
60#include <stdio.h>
61#include <sys/stat.h>
62#include <sys/errno.h>
63#include <stdarg.h>
64
65static void
66safe_printf (const char *fmt, ...)
67{
68        va_list ap;
69
70        va_start(ap, fmt);
71        if ( _System_state_Is_up( _System_state_Get() ) )
72                vfprintf( stderr, fmt, ap );
73        else
74                vprintk( fmt, ap );
75        va_end(ap);
76}
77
78static void
79safe_perror(const char *s)
80{
81        safe_printf("%s :%s\n", s, strerror(errno));
82}
83
84
85int
86BSP_i2c_initialize()
87{
88int busno, succ = 0;
89
90        /* Initialize the library */
91        if ( rtems_libi2c_initialize() ) {
92                safe_printf("Initializing I2C library failed\n");
93                return -1;
94        }
95       
96        /* Register our bus driver */
97        if ( (busno=rtems_libi2c_register_bus(
98                                        BSP_I2C_BUS0_NAME,
99                                        BSP_I2C_BUS_DESCRIPTOR) ) < 0 ) {
100                safe_perror("Registering mpc8540 i2c bus driver");
101                return -1;
102        }
103
104        /* Now register higher level drivers; note that
105         * the i2c address in the manual is actually left-shifted
106         * by one bit, i.e., as it would go on the bus.
107         */
108
109        /* Use read-only driver for VPD */
110        if ( rtems_libi2c_register_drv(
111                                BSP_I2C_VPD_EEPROM_NAME,
112                                i2c_2b_eeprom_ro_driver_descriptor,
113                                busno,
114                                BSP_VPD_I2C_ADDR) < 0 ) {
115                safe_perror("Registering i2c VPD eeprom driver failed");
116        } else {
117                succ++;
118        }
119
120        /* Use read-write driver for user eeprom -- you still might
121         * have to disable HW write-protection on your board.
122         */
123        if ( rtems_libi2c_register_drv(
124                                BSP_I2C_USR_EEPROM_NAME,
125                                i2c_2b_eeprom_driver_descriptor,
126                                busno,
127                                BSP_USR0_I2C_ADDR) < 0 ) {
128                safe_perror("Registering i2c 1st USR eeprom driver failed");
129        } else {
130                succ++;
131        }
132
133        /* Use read-write driver for user eeprom -- you still might
134         * have to disable HW write-protection on your board.
135         */
136        if ( rtems_libi2c_register_drv(
137                                BSP_I2C_USR1_EEPROM_NAME,
138                                i2c_2b_eeprom_driver_descriptor,
139                                busno,
140                                BSP_USR1_I2C_ADDR) < 0 ) {
141                safe_perror("Registering i2c 2nd USR eeprom driver failed");
142        } else {
143                succ++;
144        }
145
146        /* The thermostat */
147        if ( rtems_libi2c_register_drv(
148                                BSP_I2C_DS1621_NAME,
149                                i2c_ds1621_driver_descriptor,
150                                busno,
151                                BSP_THM_I2C_ADDR) < 0 ) {
152                safe_perror("Registering i2c ds1621 temp sensor. driver failed");
153        } else {
154                succ++;
155        }
156
157        /* Finally, as an example, register raw access to the
158         * ds1621. The driver above just reads the 8 msb of the
159         * temperature but doesn't support anything else. Using
160         * the raw device node you can write/read individual
161         * control bytes yourself and e.g., program the thermostat...
162         */
163
164        if ( mknod(
165                        BSP_I2C_DS1621_RAW_DEV_NAME,
166                        0666 | S_IFCHR,
167                        rtems_filesystem_make_dev_t(rtems_libi2c_major,
168                                  RTEMS_LIBI2C_MAKE_MINOR(busno,BSP_THM_I2C_ADDR))) ) {
169                safe_perror("Creating device node for raw ds1621 (temp. sensor) access failed");
170        } else {
171                succ++;
172        }
173
174        /* Raw access to RTC */
175        if ( mknod(
176                        BSP_I2C_DS1375_RAW_DEV_NAME,
177                        0666 | S_IFCHR,
178                        rtems_filesystem_make_dev_t(rtems_libi2c_major,
179                                  RTEMS_LIBI2C_MAKE_MINOR(busno,BSP_RTC_I2C_ADDR))) ) {
180                safe_perror("Creating device node for raw ds1375 (rtc) access failed");
181        } else {
182                succ++;
183        }
184
185        safe_printf("%i I2C devices registered\n", succ);
186        return 0;
187}
Note: See TracBrowser for help on using the repository browser.