1 // SPDX-License-Identifier: MIT
2 /*
3 * Permission is hereby granted, free of charge, to any person obtaining a
4 * copy of this software and associated documentation files (the
5 * "Software"), to deal in the Software without restriction, including
6 * without limitation the rights to use, copy, modify, merge, publish,
7 * distribute, sub license, and/or sell copies of the Software, and to
8 * permit persons to whom the Software is furnished to do so, subject to
9 * the following conditions:
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
14 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
17 * USE OR OTHER DEALINGS IN THE SOFTWARE.
18 *
19 * The above copyright notice and this permission notice (including the
20 * next paragraph) shall be included in all copies or substantial portions
21 * of the Software.
22 */
23
24 #include <drm/drm_managed.h>
25 #include <drm/drm_print.h>
26
27 #include "ast_drv.h"
28
ast_i2c_setsda(void * i2c_priv,int data)29 static void ast_i2c_setsda(void *i2c_priv, int data)
30 {
31 struct ast_i2c_chan *i2c = i2c_priv;
32 struct ast_device *ast = to_ast_device(i2c->dev);
33 int i;
34 u8 ujcrb7, jtemp;
35
36 for (i = 0; i < 0x10000; i++) {
37 ujcrb7 = ((data & 0x01) ? 0 : 1) << 2;
38 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0xf1, ujcrb7);
39 jtemp = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x04);
40 if (ujcrb7 == jtemp)
41 break;
42 }
43 }
44
ast_i2c_setscl(void * i2c_priv,int clock)45 static void ast_i2c_setscl(void *i2c_priv, int clock)
46 {
47 struct ast_i2c_chan *i2c = i2c_priv;
48 struct ast_device *ast = to_ast_device(i2c->dev);
49 int i;
50 u8 ujcrb7, jtemp;
51
52 for (i = 0; i < 0x10000; i++) {
53 ujcrb7 = ((clock & 0x01) ? 0 : 1);
54 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0xf4, ujcrb7);
55 jtemp = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x01);
56 if (ujcrb7 == jtemp)
57 break;
58 }
59 }
60
ast_i2c_getsda(void * i2c_priv)61 static int ast_i2c_getsda(void *i2c_priv)
62 {
63 struct ast_i2c_chan *i2c = i2c_priv;
64 struct ast_device *ast = to_ast_device(i2c->dev);
65 uint32_t val, val2, count, pass;
66
67 count = 0;
68 pass = 0;
69 val = (ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x20) >> 5) & 0x01;
70 do {
71 val2 = (ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x20) >> 5) & 0x01;
72 if (val == val2) {
73 pass++;
74 } else {
75 pass = 0;
76 val = (ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x20) >> 5) & 0x01;
77 }
78 } while ((pass < 5) && (count++ < 0x10000));
79
80 return val & 1 ? 1 : 0;
81 }
82
ast_i2c_getscl(void * i2c_priv)83 static int ast_i2c_getscl(void *i2c_priv)
84 {
85 struct ast_i2c_chan *i2c = i2c_priv;
86 struct ast_device *ast = to_ast_device(i2c->dev);
87 uint32_t val, val2, count, pass;
88
89 count = 0;
90 pass = 0;
91 val = (ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x10) >> 4) & 0x01;
92 do {
93 val2 = (ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x10) >> 4) & 0x01;
94 if (val == val2) {
95 pass++;
96 } else {
97 pass = 0;
98 val = (ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x10) >> 4) & 0x01;
99 }
100 } while ((pass < 5) && (count++ < 0x10000));
101
102 return val & 1 ? 1 : 0;
103 }
104
ast_i2c_release(struct drm_device * dev,void * res)105 static void ast_i2c_release(struct drm_device *dev, void *res)
106 {
107 struct ast_i2c_chan *i2c = res;
108
109 i2c_del_adapter(&i2c->adapter);
110 kfree(i2c);
111 }
112
ast_i2c_create(struct drm_device * dev)113 struct ast_i2c_chan *ast_i2c_create(struct drm_device *dev)
114 {
115 struct ast_i2c_chan *i2c;
116 int ret;
117
118 i2c = kzalloc(sizeof(struct ast_i2c_chan), GFP_KERNEL);
119 if (!i2c)
120 return NULL;
121
122 i2c->adapter.owner = THIS_MODULE;
123 i2c->adapter.class = I2C_CLASS_DDC;
124 i2c->adapter.dev.parent = dev->dev;
125 i2c->dev = dev;
126 i2c_set_adapdata(&i2c->adapter, i2c);
127 snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),
128 "AST i2c bit bus");
129 i2c->adapter.algo_data = &i2c->bit;
130
131 i2c->bit.udelay = 20;
132 i2c->bit.timeout = 2;
133 i2c->bit.data = i2c;
134 i2c->bit.setsda = ast_i2c_setsda;
135 i2c->bit.setscl = ast_i2c_setscl;
136 i2c->bit.getsda = ast_i2c_getsda;
137 i2c->bit.getscl = ast_i2c_getscl;
138 ret = i2c_bit_add_bus(&i2c->adapter);
139 if (ret) {
140 drm_err(dev, "Failed to register bit i2c\n");
141 goto out_kfree;
142 }
143
144 ret = drmm_add_action_or_reset(dev, ast_i2c_release, i2c);
145 if (ret)
146 return NULL;
147 return i2c;
148
149 out_kfree:
150 kfree(i2c);
151 return NULL;
152 }
153