1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2024, Microchip 4 */ 5 6 #include <assert.h> 7 #include <drivers/clk.h> 8 #include <drivers/clk_dt.h> 9 #include <dt_driver.h> 10 #include <string.h> 11 12 #define MCHP_PIT64B_FREQ UL(5000000) /* 5 MHz */ 13 14 static TEE_Result microchip_pit_probe(const void *fdt, int node, 15 const void *compat_data __unused) 16 { 17 TEE_Result res = TEE_ERROR_GENERIC; 18 struct clk *parent = NULL; 19 struct clk *pclk = NULL; 20 struct clk *gclk = NULL; 21 size_t i = 0; 22 23 res = clk_dt_get_by_name(fdt, node, "pclk", &pclk); 24 if (res) 25 return res; 26 27 res = clk_dt_get_by_name(fdt, node, "gclk", &gclk); 28 if (res) 29 return res; 30 31 res = clk_enable(pclk); 32 if (res) 33 panic(); 34 35 do { 36 parent = clk_get_parent_by_index(gclk, i++); 37 if (!memcmp("syspll", clk_get_name(parent), sizeof("syspll"))) 38 break; 39 } while (parent); 40 if (!parent) 41 panic(); 42 43 res = clk_set_parent(gclk, parent); 44 if (res) 45 panic(); 46 47 res = clk_set_rate(gclk, MCHP_PIT64B_FREQ); 48 if (res) 49 panic(); 50 51 return clk_enable(gclk); 52 } 53 54 static const struct dt_device_match microchip_pit_match_table[] = { 55 { .compatible = "microchip,sama7g5-pit64b" }, 56 { } 57 }; 58 59 DEFINE_DT_DRIVER(microchip_pit_dt_driver) = { 60 .name = "microchip_pit", 61 .type = DT_DRIVER_NOTYPE, 62 .match_table = microchip_pit_match_table, 63 .probe = microchip_pit_probe, 64 }; 65