blob: db660fe67ff7be807ff3ccfa14c36a7cf0e8865e [file] [log] [blame]
Angel Pons8a3453f2020-04-02 23:48:19 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Arthur Heymans24231ac2017-06-06 09:46:01 +02002
3#include <assert.h>
4#include <console/console.h>
5#include <device/device.h>
6#include <device/smbus.h>
Arthur Heymans24231ac2017-06-06 09:46:01 +02007#include "chip.h"
Arthur Heymans24231ac2017-06-06 09:46:01 +02008
9#define SMBUS_BLOCK_SIZE 32
10
11static void ck505_init(struct device *dev)
12{
13 struct drivers_i2c_ck505_config *config;
14 int dev_nregs, nregs;
15 u8 block[SMBUS_BLOCK_SIZE];
16 int i;
17
18 if (!dev->enabled || dev->path.type != DEVICE_PATH_I2C)
19 return;
20
21 config = dev->chip_info;
22
23 dev_nregs = smbus_block_read(dev, 0, sizeof(block), block);
24
25 if (dev_nregs < 0) {
26 printk(BIOS_ERR, "Failed reading ck505 configuration!\n");
27 return;
28 }
29
30 /* This means that the devicetree doesn't have to specify nregs */
31 nregs = MIN(MIN(dev_nregs, config->nregs == 0 ? SMBUS_BLOCK_SIZE
32 : config->nregs), ARRAY_SIZE(config->mask));
33
Arthur Heymans24231ac2017-06-06 09:46:01 +020034 printk(BIOS_DEBUG, "Changing %d of the %d ck505 config bytes.\n",
35 nregs, dev_nregs);
36
37 assert(ARRAY_SIZE(config->mask) == ARRAY_SIZE(config->regs));
38
39 for (i = 0; i < nregs && i < SMBUS_BLOCK_SIZE; i++)
40 block[i] = (block[i] & ~config->mask[i]) | config->regs[i];
41
42 if (smbus_block_write(dev, 0, dev_nregs, block) < 0)
43 printk(BIOS_ERR, "Failed writing ck505 configuration!\n");
44}
45
46static struct device_operations ck505_operations = {
Nico Huber2f8ba692020-04-05 14:05:24 +020047 .read_resources = noop_read_resources,
48 .set_resources = noop_set_resources,
Arthur Heymans24231ac2017-06-06 09:46:01 +020049 .init = ck505_init,
50};
51
52static void enable_dev(struct device *dev)
53{
54 dev->ops = &ck505_operations;
55}
56
57struct chip_operations drivers_i2c_ck505_ops = {
Nicholas Sudsgaardbfb11be2024-01-30 09:53:46 +090058 .name = "CK505 Clock generator",
Arthur Heymans24231ac2017-06-06 09:46:01 +020059 .enable_dev = enable_dev,
60};