blob: 9fa5da72387192467ba80c9f9f9a7f2a0adbe339 [file] [log] [blame]
Ben Skeggs6ee73862009-12-11 19:24:15 +10001#ifndef __NOUVEAU_I2C_H__
2#define __NOUVEAU_I2C_H__
3
Ben Skeggs4196faa2012-07-10 14:36:38 +10004#include <core/subdev.h>
5#include <core/device.h>
6
7#include <subdev/bios.h>
8#include <subdev/bios/i2c.h>
Ben Skeggs6ee73862009-12-11 19:24:15 +10009
Ben Skeggs486a45c2011-11-11 10:22:19 +100010#define NV_I2C_PORT(n) (0x00 + (n))
Ben Skeggs486a45c2011-11-11 10:22:19 +100011#define NV_I2C_DEFAULT(n) (0x80 + (n))
Ben Skeggs6ee73862009-12-11 19:24:15 +100012
Ben Skeggs7dcd060c2013-02-16 15:21:58 +100013#define NV_I2C_TYPE_DCBI2C(n) (0x0000 | (n))
14#define NV_I2C_TYPE_EXTDDC(e) (0x0005 | (e) << 8)
15#define NV_I2C_TYPE_EXTAUX(e) (0x0006 | (e) << 8)
16
Ben Skeggs4196faa2012-07-10 14:36:38 +100017struct nouveau_i2c_port {
Ben Skeggs7dcd060c2013-02-16 15:21:58 +100018 struct nouveau_object base;
Ben Skeggs6ee73862009-12-11 19:24:15 +100019 struct i2c_adapter adapter;
Ben Skeggs7dcd060c2013-02-16 15:21:58 +100020
Ben Skeggs486a45c2011-11-11 10:22:19 +100021 struct list_head head;
22 u8 index;
Ben Skeggs7dcd060c2013-02-16 15:21:58 +100023
24 const struct nouveau_i2c_func *func;
Ben Skeggs6ee73862009-12-11 19:24:15 +100025};
26
Ben Skeggs7dcd060c2013-02-16 15:21:58 +100027struct nouveau_i2c_func {
28 void (*acquire)(struct nouveau_i2c_port *);
29 void (*release)(struct nouveau_i2c_port *);
30
31 void (*drive_scl)(struct nouveau_i2c_port *, int);
32 void (*drive_sda)(struct nouveau_i2c_port *, int);
33 int (*sense_scl)(struct nouveau_i2c_port *);
34 int (*sense_sda)(struct nouveau_i2c_port *);
35
36 int (*aux)(struct nouveau_i2c_port *, u8, u32, u8 *, u8);
37 int (*pattern)(struct nouveau_i2c_port *, int pattern);
38 int (*lnk_ctl)(struct nouveau_i2c_port *, int nr, int bw, bool enh);
39 int (*drv_ctl)(struct nouveau_i2c_port *, int lane, int sw, int pe);
40};
41
Ilia Mirkinc8655342013-08-23 13:03:14 -040042#define nouveau_i2c_port_create(p,e,o,i,a,f,d) \
43 nouveau_i2c_port_create_((p), (e), (o), (i), (a), (f), \
Ben Skeggs7dcd060c2013-02-16 15:21:58 +100044 sizeof(**d), (void **)d)
45#define nouveau_i2c_port_destroy(p) ({ \
46 struct nouveau_i2c_port *port = (p); \
47 _nouveau_i2c_port_dtor(nv_object(i2c)); \
48})
49#define nouveau_i2c_port_init(p) \
50 nouveau_object_init(&(p)->base)
51#define nouveau_i2c_port_fini(p,s) \
52 nouveau_object_fini(&(p)->base, (s))
53
54int nouveau_i2c_port_create_(struct nouveau_object *, struct nouveau_object *,
55 struct nouveau_oclass *, u8,
Ilia Mirkinc8655342013-08-23 13:03:14 -040056 const struct i2c_algorithm *,
57 const struct nouveau_i2c_func *,
58 int, void **);
Ben Skeggs7dcd060c2013-02-16 15:21:58 +100059void _nouveau_i2c_port_dtor(struct nouveau_object *);
60#define _nouveau_i2c_port_init nouveau_object_init
61#define _nouveau_i2c_port_fini nouveau_object_fini
62
Martin Peres9e2b7342013-10-21 01:48:55 +020063struct nouveau_i2c_board_info {
64 struct i2c_board_info dev;
65 u8 udelay; /* set to 0 to use the standard delay */
66};
67
Ben Skeggs4196faa2012-07-10 14:36:38 +100068struct nouveau_i2c {
69 struct nouveau_subdev base;
Ben Skeggs6ee73862009-12-11 19:24:15 +100070
Ben Skeggs4196faa2012-07-10 14:36:38 +100071 struct nouveau_i2c_port *(*find)(struct nouveau_i2c *, u8 index);
Ben Skeggs548ddb62013-02-16 13:19:18 +100072 struct nouveau_i2c_port *(*find_type)(struct nouveau_i2c *, u16 type);
Ben Skeggs4196faa2012-07-10 14:36:38 +100073 int (*identify)(struct nouveau_i2c *, int index,
Martin Peres9e2b7342013-10-21 01:48:55 +020074 const char *what, struct nouveau_i2c_board_info *,
Ben Skeggs4196faa2012-07-10 14:36:38 +100075 bool (*match)(struct nouveau_i2c_port *,
76 struct i2c_board_info *));
77 struct list_head ports;
78};
Ben Skeggs6ee73862009-12-11 19:24:15 +100079
Ben Skeggs4196faa2012-07-10 14:36:38 +100080static inline struct nouveau_i2c *
81nouveau_i2c(void *obj)
82{
83 return (void *)nv_device(obj)->subdev[NVDEV_SUBDEV_I2C];
84}
85
Ben Skeggs7dcd060c2013-02-16 15:21:58 +100086#define nouveau_i2c_create(p,e,o,s,d) \
87 nouveau_i2c_create_((p), (e), (o), (s), sizeof(**d), (void **)d)
88#define nouveau_i2c_destroy(p) ({ \
89 struct nouveau_i2c *i2c = (p); \
90 _nouveau_i2c_dtor(nv_object(i2c)); \
91})
92#define nouveau_i2c_init(p) ({ \
93 struct nouveau_i2c *i2c = (p); \
94 _nouveau_i2c_init(nv_object(i2c)); \
95})
96#define nouveau_i2c_fini(p,s) ({ \
97 struct nouveau_i2c *i2c = (p); \
98 _nouveau_i2c_fini(nv_object(i2c), (s)); \
99})
Ben Skeggs4196faa2012-07-10 14:36:38 +1000100
Ben Skeggs7dcd060c2013-02-16 15:21:58 +1000101int nouveau_i2c_create_(struct nouveau_object *, struct nouveau_object *,
102 struct nouveau_oclass *, struct nouveau_oclass *,
103 int, void **);
104void _nouveau_i2c_dtor(struct nouveau_object *);
105int _nouveau_i2c_init(struct nouveau_object *);
106int _nouveau_i2c_fini(struct nouveau_object *, bool);
Ben Skeggs4196faa2012-07-10 14:36:38 +1000107
Ben Skeggs7dcd060c2013-02-16 15:21:58 +1000108extern struct nouveau_oclass nv04_i2c_oclass;
109extern struct nouveau_oclass nv4e_i2c_oclass;
110extern struct nouveau_oclass nv50_i2c_oclass;
111extern struct nouveau_oclass nv94_i2c_oclass;
112extern struct nouveau_oclass nvd0_i2c_oclass;
Ben Skeggs5effecd2013-02-14 17:48:58 -0500113extern struct nouveau_oclass nouveau_anx9805_sclass[];
Ben Skeggs4196faa2012-07-10 14:36:38 +1000114
115extern const struct i2c_algorithm nouveau_i2c_bit_algo;
116extern const struct i2c_algorithm nouveau_i2c_aux_algo;
117
Ben Skeggs7dcd060c2013-02-16 15:21:58 +1000118static inline int
119nv_rdi2cr(struct nouveau_i2c_port *port, u8 addr, u8 reg)
120{
121 u8 val;
122 struct i2c_msg msgs[] = {
123 { .addr = addr, .flags = 0, .len = 1, .buf = &reg },
124 { .addr = addr, .flags = I2C_M_RD, .len = 1, .buf = &val },
125 };
126
127 int ret = i2c_transfer(&port->adapter, msgs, 2);
128 if (ret != 2)
129 return -EIO;
130
131 return val;
132}
133
134static inline int
135nv_wri2cr(struct nouveau_i2c_port *port, u8 addr, u8 reg, u8 val)
136{
137 u8 buf[2] = { reg, val };
138 struct i2c_msg msgs[] = {
139 { .addr = addr, .flags = 0, .len = 2, .buf = buf },
140 };
141
142 int ret = i2c_transfer(&port->adapter, msgs, 1);
143 if (ret != 1)
144 return -EIO;
145
146 return 0;
147}
148
149static inline bool
150nv_probe_i2c(struct nouveau_i2c_port *port, u8 addr)
151{
152 return nv_rdi2cr(port, addr, 0) >= 0;
153}
154
155int nv_rdaux(struct nouveau_i2c_port *, u32 addr, u8 *data, u8 size);
156int nv_wraux(struct nouveau_i2c_port *, u32 addr, u8 *data, u8 size);
Ben Skeggs31a34aa2013-02-14 20:59:41 -0500157
Ben Skeggs4196faa2012-07-10 14:36:38 +1000158#endif