Skip to content

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
Move loop indexes into loops
Return on errors instead of loop breaks
Some variables cleanup
  • Loading branch information
klogg committed May 11, 2024
1 parent 7c2a33e commit 5692fca
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 57 deletions.
40 changes: 22 additions & 18 deletions bridge/it66121_drv.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ static int it66121_configure_afe(struct it66121_priv *priv, int clock_khz)

static int it66121_wait_ddc_ready(struct it66121_priv *priv)
{
int ret, val;
int ret;
unsigned int val;

ret = regmap_field_read_poll_timeout(priv->ddc_done, val, true, EDID_SLEEP, EDID_TIMEOUT);
if (ret)
Expand Down Expand Up @@ -221,7 +222,7 @@ static int it66121_clear_ddc_fifo(struct it66121_priv *priv)

static int it66121_abort_ddc_ops(struct it66121_priv *priv)
{
int i, ret;
int ret;

/* Prior to DDC abort command there was also a reset of HDCP:
* 1. HDCP_DESIRE clear bit CP DESIRE
Expand All @@ -233,7 +234,7 @@ static int it66121_abort_ddc_ops(struct it66121_priv *priv)
/* From original driver: According to 2009/01/15 modification by [email protected]
* do abort DDC twice
*/
for (i = 0; i < 2; i++) {
for (int i = 0; i < 2; i++) {
ret = regmap_write(priv->regmap, IT66121_DDC_COMMAND, DDC_CMD_ABORT);
if (ret)
return ret;
Expand Down Expand Up @@ -326,9 +327,11 @@ static void it66121_intr_work(struct work_struct *work_item)

static int it66121_get_edid_block(void *context, u8 *buf, unsigned int block, size_t len)
{
int i, ret, offset = block & 1 ? 128 : 0;
int ret;
size_t remain = len;
unsigned int rd_fifo_val, segment = block >> 1;
unsigned int val;
unsigned int segment = block >> 1;
unsigned int offset = block & 1 ? 128 : 0;
static const u8 header[EDID_LOSS_LEN] = { 0x00, 0xFF, 0xFF };
struct it66121_priv *priv = context;

Expand All @@ -354,35 +357,36 @@ static int it66121_get_edid_block(void *context, u8 *buf, unsigned int block, si
/* Clear DDC FIFO */
ret = it66121_clear_ddc_fifo(priv);
if (ret)
break;
return ret;

ret = regmap_write(priv->regmap, IT66121_DDC_ADDRESS, EDID_DDC_ADDR);
if (ret)
break;
return ret;

/* Account 3 bytes that will be lost */
ret = regmap_write(priv->regmap, IT66121_DDC_OFFSET, offset - EDID_LOSS_LEN);
if (ret)
break;
return ret;

ret = regmap_write(priv->regmap, IT66121_DDC_SIZE, (unsigned int)size);
if (ret)
break;
return ret;
ret = regmap_write(priv->regmap, IT66121_DDC_SEGMENT, segment);
if (ret)
break;
return ret;
ret = regmap_write(priv->regmap, IT66121_DDC_COMMAND, DDC_CMD_EDID_READ);
if (ret)
break;
return ret;

/* Deduct lost bytes when reading from FIFO */
size -= EDID_LOSS_LEN;

for (i = 0; i < size; i++) {
ret = regmap_read(priv->regmap, IT66121_DDC_RD_FIFO, &rd_fifo_val);
for (int i = 0; i < size; i++) {
ret = regmap_read(priv->regmap, IT66121_DDC_RD_FIFO, &val);
if (ret)
return ret;

*(buf++) = rd_fifo_val & 0xFF;
*(buf++) = val & 0xFF;
}

remain -= size;
Expand Down Expand Up @@ -601,7 +605,7 @@ static void it66121_bridge_mode_set(struct drm_bridge *bridge, const struct drm_
const struct drm_display_mode *adjusted_mode)

{
int i, ret;
int ret;
ssize_t frame_size;
struct it66121_priv *priv = container_of(bridge, struct it66121_priv, bridge);
u8 buf[HDMI_INFOFRAME_SIZE(AVI)];
Expand Down Expand Up @@ -658,7 +662,7 @@ static void it66121_bridge_mode_set(struct drm_bridge *bridge, const struct drm_
}

/* Write new AVI infoframe packet */
for (i = 0; i < HDMI_AVI_INFOFRAME_SIZE; i++) {
for (int i = 0; i < HDMI_AVI_INFOFRAME_SIZE; i++) {
ret = regmap_write(priv->regmap, aviinfo_reg[i],
buf[i + HDMI_INFOFRAME_HEADER_SIZE]);
if (ret) {
Expand Down Expand Up @@ -764,7 +768,7 @@ static int it66121_regs_init(struct it66121_priv *priv, struct i2c_client *clien

static int it66121_i2c_probe(struct i2c_adapter *adapter, unsigned short address)
{
int i, ret;
int ret;
u8 id_regs[] = { IT66121_VENDOR_ID_1, IT66121_VENDOR_ID_2, IT66121_DEVICE_ID_1,
IT66121_DEVICE_ID_2 };
union {
Expand All @@ -784,7 +788,7 @@ static int it66121_i2c_probe(struct i2c_adapter *adapter, unsigned short address
return -ENODEV;
}

for (i = 0; i < ARRAY_SIZE(id_regs); i++) {
for (int i = 0; i < ARRAY_SIZE(id_regs); i++) {
struct i2c_msg msgs[] = {
{ .addr = address, .flags = 0, .len = 1, .buf = &id_regs[i] },
{ .addr = address, .flags = I2C_M_RD, .len = 1, .buf = &id.b[i] }
Expand Down
30 changes: 13 additions & 17 deletions fl2000_drm.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ static const u32 fl2000_pixel_formats[] = {

static unsigned int fl2000_get_bytes_pix(enum usb_device_speed speed, unsigned int pixclock)
{
unsigned int bytes_pix, max_bw;
unsigned int bytes_pix;
unsigned int max_bw;

/* Calculate maximum bandwidth, bytes per second */
switch (speed) {
Expand Down Expand Up @@ -151,12 +152,11 @@ static inline u32 fl2000_pll_get_divisor(u64 clock_mil, u32 vco_clk, u64 *min_pp
100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115,
116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128
};
unsigned int divisor_idx;
u32 best_divisor = 0;

/* Iterate over array */
for (divisor_idx = 0; divisor_idx < ARRAY_SIZE(divisor_arr); divisor_idx++) {
u32 divisor = divisor_arr[divisor_idx];
for (int i = 0; i < ARRAY_SIZE(divisor_arr); i++) {
u32 divisor = divisor_arr[i];
u64 ppm_err = fl2000_pll_ppm_err(clock_mil, vco_clk, divisor);

if (ppm_err < *min_ppm_err) {
Expand All @@ -173,12 +173,10 @@ static u64 fl2000_pll_calc(u64 clock_mil, struct fl2000_pll *pll, u32 *clock_cal
{
static const u32 prescaler_max = 2;
static const u32 multiplier_max = 128;
u32 prescaler;
u32 multiplier;
u64 min_ppm_err = (u64)(-1);

for (prescaler = 1; prescaler <= prescaler_max; prescaler++)
for (multiplier = 1; multiplier <= multiplier_max; multiplier++) {
for (u32 prescaler = 1; prescaler <= prescaler_max; prescaler++)
for (u32 multiplier = 1; multiplier <= multiplier_max; multiplier++) {
/* Do not need precision here yet, no 10^6 multiply */
u32 vco_clk = FL2000_XTAL / prescaler * multiplier;
u32 divisor;
Expand All @@ -205,7 +203,6 @@ static u64 fl2000_pll_calc(u64 clock_mil, struct fl2000_pll *pll, u32 *clock_cal
*clock_calculated = vco_clk / divisor;
}

/* No exact PLL settings found for requested clock */
return min_ppm_err;
}

Expand All @@ -217,17 +214,15 @@ static int fl2000_mode_calc(const struct drm_display_mode *mode,
u64 clock_mil_adjusted;
const u64 clock_mil = (u64)mode->clock * 1000 * FL2000_PLL_PRECISION;
const int max_h_adjustment = 10;
int s, m;
int d = 0;

if (mode->clock * 1000 > FL2000_MAX_PIXCLOCK)
return -1;

/* Try to match pixel clock slightly adjusting htotal value */
for (m = 0, s = 0; m <= max_h_adjustment * 2; m++, s = -s) {
/* 0, -1, 1, -2, 2, -3, 3, -3, 4, -4, 5, -5, ... */
d += m * s;

/* Try to match pixel clock slightly adjusting htotal value, sequence is:
0, -1, 1, -2, 2, -3, 3, -3, 4, -4, 5, -5, ...
Here, 's' is used for sign, 'm' is used for modulo, and 'd' is the adjustment value
*/
for (int m = 0, s = 0, d = 0; m <= max_h_adjustment * 2; m++, s = -s, d += m * s) {
/* Maximum pixel clock 1GHz, or 10^9Hz. Multiply by 10^6 we get 10^15Hz. Assume
* maximum htotal is 10000 pix (no way) we get 10^19 max value and using u64 which
* is 1.8*10^19 no overflow can occur. Assume all this was checked before
Expand Down Expand Up @@ -302,7 +297,8 @@ static void fl2000_display_disable(struct drm_simple_display_pipe *pipe)

static void fb2000_dirty(struct drm_framebuffer *fb, struct drm_rect *rect)
{
int idx, ret;
int ret;
int idx;
struct drm_device *drm = fb->dev;
struct fl2000_drm_if *drm_if = drm->dev_private;
struct drm_gem_dma_object *dma_obj = drm_fb_dma_get_gem_obj(fb, 0);
Expand Down
3 changes: 1 addition & 2 deletions fl2000_drv.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ static struct component_master_ops fl2000_master_ops = {

static int fl2000_compare(struct device *dev, void *data)
{
int i;
struct i2c_client *client = i2c_verify_client(dev);
static const char *const fl2000_supported_bridges[] = {
"it66121", /* IT66121 driver name*/
Expand All @@ -52,7 +51,7 @@ static int fl2000_compare(struct device *dev, void *data)
return 0;

/* Check this is a supported DRM bridge */
for (i = 0; i < ARRAY_SIZE(fl2000_supported_bridges); i++)
for (int i = 0; i < ARRAY_SIZE(fl2000_supported_bridges); i++)
if (!strncmp(fl2000_supported_bridges[i], client->name, sizeof(client->name)))
return 1; /* Must be not 0 for success */

Expand Down
3 changes: 2 additions & 1 deletion fl2000_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ static int fl2000_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, in
int ret;
bool read;
u16 addr;
u8 idx, offset;
u8 idx;
u8 offset;
union {
u32 w;
u8 b[4];
Expand Down
33 changes: 14 additions & 19 deletions fl2000_streaming.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,11 @@ struct fl2000_stream {

static void fl2000_free_sb(struct fl2000_stream_buf *sb)
{
int i;

vunmap(sb->vaddr);

sg_free_table(&sb->sgt);

for (i = 0; i < sb->nr_pages && sb->pages[i]; i++)
for (int i = 0; i < sb->nr_pages && sb->pages[i]; i++)
__free_page(sb->pages[i]);

kfree(sb->pages);
Expand All @@ -66,7 +64,7 @@ static void fl2000_free_sb(struct fl2000_stream_buf *sb)

static struct fl2000_stream_buf *fl2000_alloc_sb(unsigned int size)
{
int i, ret;
int ret;
struct fl2000_stream_buf *sb;
unsigned int nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;

Expand All @@ -80,7 +78,7 @@ static struct fl2000_stream_buf *fl2000_alloc_sb(unsigned int size)
if (!sb->pages)
goto error;

for (i = 0; i < nr_pages; i++) {
for (int i = 0; i < nr_pages; i++) {
sb->pages[i] = alloc_page(GFP_KERNEL);
if (!sb->pages[i])
goto error;
Expand All @@ -107,7 +105,8 @@ static struct fl2000_stream_buf *fl2000_alloc_sb(unsigned int size)

static void fl2000_stream_put_buffers(struct fl2000_stream *stream)
{
struct fl2000_stream_buf *cur_sb, *temp_sb;
struct fl2000_stream_buf *cur_sb;
struct fl2000_stream_buf *temp_sb;

list_for_each_entry_safe(cur_sb, temp_sb, &stream->render_list, list) {
list_del(&cur_sb->list);
Expand All @@ -117,12 +116,12 @@ static void fl2000_stream_put_buffers(struct fl2000_stream *stream)

static int fl2000_stream_get_buffers(struct fl2000_stream *stream, unsigned int size)
{
int i, ret;
int ret;
struct fl2000_stream_buf *cur_sb;

BUG_ON(!list_empty(&stream->render_list));

for (i = 0; i < FL2000_SB_NUM; i++) {
for (int i = 0; i < FL2000_SB_NUM; i++) {
cur_sb = fl2000_alloc_sb(size);
if (!cur_sb) {
ret = -ENOMEM;
Expand Down Expand Up @@ -178,7 +177,8 @@ static void fl2000_stream_work(struct work_struct *work)
int ret;
struct fl2000_stream *stream = container_of(work, struct fl2000_stream, work);
struct usb_device *usb_dev = stream->usb_dev;
struct fl2000_stream_buf *cur_sb, *last_sb;
struct fl2000_stream_buf *cur_sb;
struct fl2000_stream_buf *last_sb;
struct urb *data_urb;

while (stream->enabled) {
Expand Down Expand Up @@ -240,9 +240,9 @@ static void fl2000_stream_work(struct work_struct *work)

static void fl2000_xrgb888_to_rgb888_line(u8 *dbuf, u32 *sbuf, u32 pixels)
{
unsigned int x, xx = 0;
unsigned int xx = 0;

for (x = 0; x < pixels; x++) {
for (unsigned int x = 0; x < pixels; x++) {
dbuf[xx++ ^ 4] = (sbuf[x] & 0x000000FF) >> 0;
dbuf[xx++ ^ 4] = (sbuf[x] & 0x0000FF00) >> 8;
dbuf[xx++ ^ 4] = (sbuf[x] & 0x00FF0000) >> 16;
Expand All @@ -251,9 +251,7 @@ static void fl2000_xrgb888_to_rgb888_line(u8 *dbuf, u32 *sbuf, u32 pixels)

static void fl2000_xrgb888_to_rgb565_line(u16 *dbuf, u32 *sbuf, u32 pixels)
{
unsigned int x;

for (x = 0; x < pixels; x++) {
for (unsigned int x = 0; x < pixels; x++) {
u16 val565 = ((sbuf[x] & 0x00F80000) >> 8) | ((sbuf[x] & 0x0000FC00) >> 5) |
((sbuf[x] & 0x000000F8) >> 3);
dbuf[x ^ 2] = val565;
Expand All @@ -264,7 +262,6 @@ void fl2000_stream_compress(struct fl2000_stream *stream, void *src, unsigned in
unsigned int width, unsigned int pitch)
{
struct fl2000_stream_buf *cur_sb;
unsigned int y;
void *dst;
u32 dst_line_len;

Expand All @@ -276,7 +273,7 @@ void fl2000_stream_compress(struct fl2000_stream *stream, void *src, unsigned in
dst = cur_sb->vaddr;
dst_line_len = width * stream->bytes_pix;

for (y = 0; y < height; y++) {
for (unsigned int y = 0; y < height; y++) {
switch (stream->bytes_pix) {
case 2:
fl2000_xrgb888_to_rgb565_line(dst, src, width);
Expand Down Expand Up @@ -328,16 +325,14 @@ int fl2000_stream_mode_set(struct fl2000_stream *stream, int pixels, u32 bytes_p

int fl2000_stream_enable(struct fl2000_stream *stream)
{
int i;

BUG_ON(list_empty(&stream->transmit_list));

sema_init(&stream->work_sem, 0);
stream->enabled = true;
queue_work(stream->work_queue, &stream->work);

/* Kick transmit workqueue with minimum buffers submitted */
for (i = 0; i < FL2000_SB_MIN; i++)
for (int i = 0; i < FL2000_SB_MIN; i++)
up(&stream->work_sem);

return 0;
Expand Down

0 comments on commit 5692fca

Please sign in to comment.