xref: /netbsd-src/sys/dev/fdt/fdt_i2c.c (revision eb3ed4d4813f5326310f194496cf04b5b1d83ca3)
1*eb3ed4d4Sskrll /* $NetBSD: fdt_i2c.c,v 1.12 2022/02/23 07:55:55 skrll Exp $ */
27d3ef0b6Sjmcneill 
37d3ef0b6Sjmcneill /*-
47d3ef0b6Sjmcneill  * Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca>
57d3ef0b6Sjmcneill  * All rights reserved.
67d3ef0b6Sjmcneill  *
77d3ef0b6Sjmcneill  * Redistribution and use in source and binary forms, with or without
87d3ef0b6Sjmcneill  * modification, are permitted provided that the following conditions
97d3ef0b6Sjmcneill  * are met:
107d3ef0b6Sjmcneill  * 1. Redistributions of source code must retain the above copyright
117d3ef0b6Sjmcneill  *    notice, this list of conditions and the following disclaimer.
127d3ef0b6Sjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
137d3ef0b6Sjmcneill  *    notice, this list of conditions and the following disclaimer in the
147d3ef0b6Sjmcneill  *    documentation and/or other materials provided with the distribution.
157d3ef0b6Sjmcneill  *
167d3ef0b6Sjmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
177d3ef0b6Sjmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
187d3ef0b6Sjmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
197d3ef0b6Sjmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
207d3ef0b6Sjmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
217d3ef0b6Sjmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
227d3ef0b6Sjmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
237d3ef0b6Sjmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
247d3ef0b6Sjmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
257d3ef0b6Sjmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
267d3ef0b6Sjmcneill  * SUCH DAMAGE.
277d3ef0b6Sjmcneill  */
287d3ef0b6Sjmcneill 
297d3ef0b6Sjmcneill #include <sys/cdefs.h>
30*eb3ed4d4Sskrll __KERNEL_RCSID(0, "$NetBSD: fdt_i2c.c,v 1.12 2022/02/23 07:55:55 skrll Exp $");
317d3ef0b6Sjmcneill 
327d3ef0b6Sjmcneill #include <sys/param.h>
337d3ef0b6Sjmcneill #include <sys/bus.h>
347d3ef0b6Sjmcneill #include <sys/kmem.h>
35233c5d85Sjmcneill #include <sys/queue.h>
367d3ef0b6Sjmcneill 
377d3ef0b6Sjmcneill #include <libfdt.h>
387d3ef0b6Sjmcneill #include <dev/fdt/fdtvar.h>
397d3ef0b6Sjmcneill 
407d3ef0b6Sjmcneill struct fdtbus_i2c_controller {
4121b71bc0Sthorpej 	i2c_tag_t i2c_tag;
427d3ef0b6Sjmcneill 	int i2c_phandle;
437d3ef0b6Sjmcneill 
44233c5d85Sjmcneill 	LIST_ENTRY(fdtbus_i2c_controller) i2c_next;
457d3ef0b6Sjmcneill };
467d3ef0b6Sjmcneill 
47233c5d85Sjmcneill static LIST_HEAD(, fdtbus_i2c_controller) fdtbus_i2c_controllers =
48233c5d85Sjmcneill     LIST_HEAD_INITIALIZER(fdtbus_i2c_controllers);
497d3ef0b6Sjmcneill 
507d3ef0b6Sjmcneill int
fdtbus_register_i2c_controller(i2c_tag_t tag,int phandle)5121b71bc0Sthorpej fdtbus_register_i2c_controller(i2c_tag_t tag, int phandle)
527d3ef0b6Sjmcneill {
537d3ef0b6Sjmcneill 	struct fdtbus_i2c_controller *i2c;
547d3ef0b6Sjmcneill 
557d3ef0b6Sjmcneill 	i2c = kmem_alloc(sizeof(*i2c), KM_SLEEP);
5621b71bc0Sthorpej 	i2c->i2c_tag = tag;
577d3ef0b6Sjmcneill 	i2c->i2c_phandle = phandle;
587d3ef0b6Sjmcneill 
59233c5d85Sjmcneill 	LIST_INSERT_HEAD(&fdtbus_i2c_controllers, i2c, i2c_next);
607d3ef0b6Sjmcneill 
617d3ef0b6Sjmcneill 	return 0;
627d3ef0b6Sjmcneill }
637d3ef0b6Sjmcneill 
647d3ef0b6Sjmcneill static struct fdtbus_i2c_controller *
fdtbus_get_i2c_controller(int phandle)657d3ef0b6Sjmcneill fdtbus_get_i2c_controller(int phandle)
667d3ef0b6Sjmcneill {
677d3ef0b6Sjmcneill 	struct fdtbus_i2c_controller *i2c;
687d3ef0b6Sjmcneill 
69233c5d85Sjmcneill 	LIST_FOREACH(i2c, &fdtbus_i2c_controllers, i2c_next) {
70233c5d85Sjmcneill 		if (i2c->i2c_phandle == phandle)
717d3ef0b6Sjmcneill 			return i2c;
727d3ef0b6Sjmcneill 	}
737d3ef0b6Sjmcneill 
747d3ef0b6Sjmcneill 	return NULL;
757d3ef0b6Sjmcneill }
767d3ef0b6Sjmcneill 
777d3ef0b6Sjmcneill i2c_tag_t
fdtbus_i2c_get_tag(int phandle)78*eb3ed4d4Sskrll fdtbus_i2c_get_tag(int phandle)
797d3ef0b6Sjmcneill {
807d3ef0b6Sjmcneill 	struct fdtbus_i2c_controller *i2c;
817d3ef0b6Sjmcneill 
827d3ef0b6Sjmcneill 	i2c = fdtbus_get_i2c_controller(phandle);
837d3ef0b6Sjmcneill 	if (i2c == NULL)
847d3ef0b6Sjmcneill 		return NULL;
857d3ef0b6Sjmcneill 
8621b71bc0Sthorpej 	return i2c->i2c_tag;
877d3ef0b6Sjmcneill }
882ed90eabSjmcneill 
89a9d03646Sjmcneill i2c_tag_t
fdtbus_i2c_acquire(int phandle,const char * prop)90a9d03646Sjmcneill fdtbus_i2c_acquire(int phandle, const char *prop)
91a9d03646Sjmcneill {
92a9d03646Sjmcneill 	int i2c_phandle;
93a9d03646Sjmcneill 
94a9d03646Sjmcneill 	i2c_phandle = fdtbus_get_phandle(phandle, prop);
95a9d03646Sjmcneill 	if (i2c_phandle == -1)
96a9d03646Sjmcneill 		return NULL;
97a9d03646Sjmcneill 
98*eb3ed4d4Sskrll 	return fdtbus_i2c_get_tag(i2c_phandle);
99a9d03646Sjmcneill }
100a9d03646Sjmcneill 
1012ed90eabSjmcneill device_t
fdtbus_attach_i2cbus(device_t dev,int phandle,i2c_tag_t tag,cfprint_t print)1022ed90eabSjmcneill fdtbus_attach_i2cbus(device_t dev, int phandle, i2c_tag_t tag, cfprint_t print)
1032ed90eabSjmcneill {
1042ed90eabSjmcneill 	struct i2cbus_attach_args iba;
10531eb2122Sjakllsch 	prop_dictionary_t devs, props;
106a7eb508fSthorpej 	device_t ret;
1072ed90eabSjmcneill 	u_int address_cells;
1082ed90eabSjmcneill 
1092ed90eabSjmcneill 	devs = prop_dictionary_create();
1102ed90eabSjmcneill 	if (of_getprop_uint32(phandle, "#address-cells", &address_cells))
1112ed90eabSjmcneill 		address_cells = 1;
1122ed90eabSjmcneill 
1132ed90eabSjmcneill 	of_enter_i2c_devs(devs, phandle, address_cells * 4, 0);
1142ed90eabSjmcneill 
1152ed90eabSjmcneill 	memset(&iba, 0, sizeof(iba));
1162ed90eabSjmcneill 	iba.iba_tag = tag;
1172ed90eabSjmcneill 	iba.iba_child_devices = prop_dictionary_get(devs, "i2c-child-devices");
1182ed90eabSjmcneill 	if (iba.iba_child_devices)
1192ed90eabSjmcneill 		prop_object_retain(iba.iba_child_devices);
1202ed90eabSjmcneill 	prop_object_release(devs);
1212ed90eabSjmcneill 
12231eb2122Sjakllsch 	props = device_properties(dev);
1238a919d64Sthorpej 	prop_dictionary_set_bool(props, "i2c-no-indirect-config", true);
12431eb2122Sjakllsch 
1252685996bSthorpej 	ret = config_found(dev, &iba, print,
126c7fb772bSthorpej 	    CFARGS(.iattr = "i2cbus"));
127a7eb508fSthorpej 	if (iba.iba_child_devices)
128a7eb508fSthorpej 		prop_object_release(iba.iba_child_devices);
129a7eb508fSthorpej 
130a7eb508fSthorpej 	return ret;
1312ed90eabSjmcneill }
132