Skip to content

Commit d0a0b6c

Browse files
author
Thomas Zimmermann
committed
drm/ast: Move I2C code into separate source file
Move I2C code into its own source file. Makes the mode-setting code a little less convoluted. v3: * fix SPDX tag to say 'SPDX-License-Identifier' Signed-off-by: Thomas Zimmermann <[email protected]> Acked-by: Maxime Ripard <[email protected]> Link: https://patchwork.freedesktop.org/patch/msgid/[email protected]
1 parent a2cce09 commit d0a0b6c

File tree

4 files changed

+156
-129
lines changed

4 files changed

+156
-129
lines changed

drivers/gpu/drm/ast/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
# Makefile for the drm device driver. This driver provides support for the
44
# Direct Rendering Infrastructure (DRI) in XFree86 4.1.0 and higher.
55

6-
ast-y := ast_drv.o ast_main.o ast_mm.o ast_mode.o ast_post.o ast_dp501.o
6+
ast-y := ast_drv.o ast_i2c.o ast_main.o ast_mm.o ast_mode.o ast_post.o ast_dp501.o
77

88
obj-$(CONFIG_DRM_AST) := ast.o

drivers/gpu/drm/ast/ast_drv.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,4 +357,7 @@ bool ast_dp501_read_edid(struct drm_device *dev, u8 *ediddata);
357357
u8 ast_get_dp501_max_clk(struct drm_device *dev);
358358
void ast_init_3rdtx(struct drm_device *dev);
359359

360+
/* ast_i2c.c */
361+
struct ast_i2c_chan *ast_i2c_create(struct drm_device *dev);
362+
360363
#endif

drivers/gpu/drm/ast/ast_i2c.c

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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+
29+
static void ast_i2c_setsda(void *i2c_priv, int data)
30+
{
31+
struct ast_i2c_chan *i2c = i2c_priv;
32+
struct ast_private *ast = to_ast_private(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+
45+
static void ast_i2c_setscl(void *i2c_priv, int clock)
46+
{
47+
struct ast_i2c_chan *i2c = i2c_priv;
48+
struct ast_private *ast = to_ast_private(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+
61+
static int ast_i2c_getsda(void *i2c_priv)
62+
{
63+
struct ast_i2c_chan *i2c = i2c_priv;
64+
struct ast_private *ast = to_ast_private(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+
83+
static int ast_i2c_getscl(void *i2c_priv)
84+
{
85+
struct ast_i2c_chan *i2c = i2c_priv;
86+
struct ast_private *ast = to_ast_private(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+
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+
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+
}

drivers/gpu/drm/ast/ast_mode.c

Lines changed: 0 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,13 @@
4040
#include <drm/drm_gem_atomic_helper.h>
4141
#include <drm/drm_gem_framebuffer_helper.h>
4242
#include <drm/drm_gem_vram_helper.h>
43-
#include <drm/drm_managed.h>
4443
#include <drm/drm_plane_helper.h>
4544
#include <drm/drm_probe_helper.h>
4645
#include <drm/drm_simple_kms_helper.h>
4746

4847
#include "ast_drv.h"
4948
#include "ast_tables.h"
5049

51-
static struct ast_i2c_chan *ast_i2c_create(struct drm_device *dev);
52-
5350
static inline void ast_load_palette_index(struct ast_private *ast,
5451
u8 index, u8 red, u8 green,
5552
u8 blue)
@@ -1408,128 +1405,3 @@ int ast_mode_config_init(struct ast_private *ast)
14081405

14091406
return 0;
14101407
}
1411-
1412-
static int get_clock(void *i2c_priv)
1413-
{
1414-
struct ast_i2c_chan *i2c = i2c_priv;
1415-
struct ast_private *ast = to_ast_private(i2c->dev);
1416-
uint32_t val, val2, count, pass;
1417-
1418-
count = 0;
1419-
pass = 0;
1420-
val = (ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x10) >> 4) & 0x01;
1421-
do {
1422-
val2 = (ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x10) >> 4) & 0x01;
1423-
if (val == val2) {
1424-
pass++;
1425-
} else {
1426-
pass = 0;
1427-
val = (ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x10) >> 4) & 0x01;
1428-
}
1429-
} while ((pass < 5) && (count++ < 0x10000));
1430-
1431-
return val & 1 ? 1 : 0;
1432-
}
1433-
1434-
static int get_data(void *i2c_priv)
1435-
{
1436-
struct ast_i2c_chan *i2c = i2c_priv;
1437-
struct ast_private *ast = to_ast_private(i2c->dev);
1438-
uint32_t val, val2, count, pass;
1439-
1440-
count = 0;
1441-
pass = 0;
1442-
val = (ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x20) >> 5) & 0x01;
1443-
do {
1444-
val2 = (ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x20) >> 5) & 0x01;
1445-
if (val == val2) {
1446-
pass++;
1447-
} else {
1448-
pass = 0;
1449-
val = (ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x20) >> 5) & 0x01;
1450-
}
1451-
} while ((pass < 5) && (count++ < 0x10000));
1452-
1453-
return val & 1 ? 1 : 0;
1454-
}
1455-
1456-
static void set_clock(void *i2c_priv, int clock)
1457-
{
1458-
struct ast_i2c_chan *i2c = i2c_priv;
1459-
struct ast_private *ast = to_ast_private(i2c->dev);
1460-
int i;
1461-
u8 ujcrb7, jtemp;
1462-
1463-
for (i = 0; i < 0x10000; i++) {
1464-
ujcrb7 = ((clock & 0x01) ? 0 : 1);
1465-
ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0xf4, ujcrb7);
1466-
jtemp = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x01);
1467-
if (ujcrb7 == jtemp)
1468-
break;
1469-
}
1470-
}
1471-
1472-
static void set_data(void *i2c_priv, int data)
1473-
{
1474-
struct ast_i2c_chan *i2c = i2c_priv;
1475-
struct ast_private *ast = to_ast_private(i2c->dev);
1476-
int i;
1477-
u8 ujcrb7, jtemp;
1478-
1479-
for (i = 0; i < 0x10000; i++) {
1480-
ujcrb7 = ((data & 0x01) ? 0 : 1) << 2;
1481-
ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0xf1, ujcrb7);
1482-
jtemp = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x04);
1483-
if (ujcrb7 == jtemp)
1484-
break;
1485-
}
1486-
}
1487-
1488-
static void ast_i2c_release(struct drm_device *dev, void *res)
1489-
{
1490-
struct ast_i2c_chan *i2c = res;
1491-
1492-
i2c_del_adapter(&i2c->adapter);
1493-
kfree(i2c);
1494-
}
1495-
1496-
static struct ast_i2c_chan *ast_i2c_create(struct drm_device *dev)
1497-
{
1498-
struct ast_i2c_chan *i2c;
1499-
int ret;
1500-
1501-
i2c = kzalloc(sizeof(struct ast_i2c_chan), GFP_KERNEL);
1502-
if (!i2c)
1503-
return NULL;
1504-
1505-
i2c->adapter.owner = THIS_MODULE;
1506-
i2c->adapter.class = I2C_CLASS_DDC;
1507-
i2c->adapter.dev.parent = dev->dev;
1508-
i2c->dev = dev;
1509-
i2c_set_adapdata(&i2c->adapter, i2c);
1510-
snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),
1511-
"AST i2c bit bus");
1512-
i2c->adapter.algo_data = &i2c->bit;
1513-
1514-
i2c->bit.udelay = 20;
1515-
i2c->bit.timeout = 2;
1516-
i2c->bit.data = i2c;
1517-
i2c->bit.setsda = set_data;
1518-
i2c->bit.setscl = set_clock;
1519-
i2c->bit.getsda = get_data;
1520-
i2c->bit.getscl = get_clock;
1521-
ret = i2c_bit_add_bus(&i2c->adapter);
1522-
if (ret) {
1523-
drm_err(dev, "Failed to register bit i2c\n");
1524-
goto out_kfree;
1525-
}
1526-
1527-
ret = drmm_add_action_or_reset(dev, ast_i2c_release, i2c);
1528-
if (ret)
1529-
return NULL;
1530-
return i2c;
1531-
1532-
out_kfree:
1533-
kfree(i2c);
1534-
return NULL;
1535-
}

0 commit comments

Comments
 (0)