1 // SPDX-License-Identifier: MIT
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:
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.
19 * The above copyright notice and this permission notice (including the
20 * next paragraph) shall be included in all copies or substantial portions
24 #include <drm/drm_managed.h>
25 #include <drm/drm_print.h>
29 static void ast_i2c_setsda(void *i2c_priv, int data)
31 struct ast_i2c_chan *i2c = i2c_priv;
32 struct ast_device *ast = to_ast_device(i2c->dev);
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);
45 static void ast_i2c_setscl(void *i2c_priv, int clock)
47 struct ast_i2c_chan *i2c = i2c_priv;
48 struct ast_device *ast = to_ast_device(i2c->dev);
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);
61 static int ast_i2c_getsda(void *i2c_priv)
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;
69 val = (ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x20) >> 5) & 0x01;
71 val2 = (ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x20) >> 5) & 0x01;
76 val = (ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x20) >> 5) & 0x01;
78 } while ((pass < 5) && (count++ < 0x10000));
80 return val & 1 ? 1 : 0;
83 static int ast_i2c_getscl(void *i2c_priv)
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;
91 val = (ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x10) >> 4) & 0x01;
93 val2 = (ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x10) >> 4) & 0x01;
98 val = (ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x10) >> 4) & 0x01;
100 } while ((pass < 5) && (count++ < 0x10000));
102 return val & 1 ? 1 : 0;
105 static void ast_i2c_release(struct drm_device *dev, void *res)
107 struct ast_i2c_chan *i2c = res;
109 i2c_del_adapter(&i2c->adapter);
113 struct ast_i2c_chan *ast_i2c_create(struct drm_device *dev)
115 struct ast_i2c_chan *i2c;
118 i2c = kzalloc(sizeof(struct ast_i2c_chan), GFP_KERNEL);
122 i2c->adapter.owner = THIS_MODULE;
123 i2c->adapter.class = I2C_CLASS_DDC;
124 i2c->adapter.dev.parent = dev->dev;
126 i2c_set_adapdata(&i2c->adapter, i2c);
127 snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),
129 i2c->adapter.algo_data = &i2c->bit;
131 i2c->bit.udelay = 20;
132 i2c->bit.timeout = 2;
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);
140 drm_err(dev, "Failed to register bit i2c\n");
144 ret = drmm_add_action_or_reset(dev, ast_i2c_release, i2c);