spi.c (bdc7ca008e1f5539e891187032cb2cbbc3decb5e) spi.c (fb51601bdf3a761ccd3f3d9dc6c03064f10f23aa)
1// SPDX-License-Identifier: GPL-2.0-or-later
2// SPI init/core code
3//
4// Copyright (C) 2005 David Brownell
5// Copyright (C) 2008 Secret Lab Technologies Ltd.
6
7#include <linux/kernel.h>
8#include <linux/device.h>

--- 803 unchanged lines hidden (view full) ---

812 mutex_unlock(&board_lock);
813 }
814
815 return 0;
816}
817
818/*-------------------------------------------------------------------------*/
819
1// SPDX-License-Identifier: GPL-2.0-or-later
2// SPI init/core code
3//
4// Copyright (C) 2005 David Brownell
5// Copyright (C) 2008 Secret Lab Technologies Ltd.
6
7#include <linux/kernel.h>
8#include <linux/device.h>

--- 803 unchanged lines hidden (view full) ---

812 mutex_unlock(&board_lock);
813 }
814
815 return 0;
816}
817
818/*-------------------------------------------------------------------------*/
819
820/* Core methods for SPI resource management */
821
822/**
823 * spi_res_alloc - allocate a spi resource that is life-cycle managed
824 * during the processing of a spi_message while using
825 * spi_transfer_one
826 * @spi: the spi device for which we allocate memory
827 * @release: the release code to execute for this resource
828 * @size: size to alloc and return
829 * @gfp: GFP allocation flags
830 *
831 * Return: the pointer to the allocated data
832 *
833 * This may get enhanced in the future to allocate from a memory pool
834 * of the @spi_device or @spi_controller to avoid repeated allocations.
835 */
836void *spi_res_alloc(struct spi_device *spi,
837 spi_res_release_t release,
838 size_t size, gfp_t gfp)
839{
840 struct spi_res *sres;
841
842 sres = kzalloc(sizeof(*sres) + size, gfp);
843 if (!sres)
844 return NULL;
845
846 INIT_LIST_HEAD(&sres->entry);
847 sres->release = release;
848
849 return sres->data;
850}
851EXPORT_SYMBOL_GPL(spi_res_alloc);
852
853/**
854 * spi_res_free - free an spi resource
855 * @res: pointer to the custom data of a resource
856 *
857 */
858void spi_res_free(void *res)
859{
860 struct spi_res *sres = container_of(res, struct spi_res, data);
861
862 if (!res)
863 return;
864
865 WARN_ON(!list_empty(&sres->entry));
866 kfree(sres);
867}
868EXPORT_SYMBOL_GPL(spi_res_free);
869
870/**
871 * spi_res_add - add a spi_res to the spi_message
872 * @message: the spi message
873 * @res: the spi_resource
874 */
875void spi_res_add(struct spi_message *message, void *res)
876{
877 struct spi_res *sres = container_of(res, struct spi_res, data);
878
879 WARN_ON(!list_empty(&sres->entry));
880 list_add_tail(&sres->entry, &message->resources);
881}
882EXPORT_SYMBOL_GPL(spi_res_add);
883
884/**
885 * spi_res_release - release all spi resources for this message
886 * @ctlr: the @spi_controller
887 * @message: the @spi_message
888 */
889void spi_res_release(struct spi_controller *ctlr, struct spi_message *message)
890{
891 struct spi_res *res, *tmp;
892
893 list_for_each_entry_safe_reverse(res, tmp, &message->resources, entry) {
894 if (res->release)
895 res->release(ctlr, message, res->data);
896
897 list_del(&res->entry);
898
899 kfree(res);
900 }
901}
902EXPORT_SYMBOL_GPL(spi_res_release);
903
904/*-------------------------------------------------------------------------*/
905
820static void spi_set_cs(struct spi_device *spi, bool enable, bool force)
821{
822 bool activate = enable;
823
824 /*
825 * Avoid calling into the driver (or doing delays) if the chip select
826 * isn't actually changing from the last time this was called.
827 */

--- 2202 unchanged lines hidden (view full) ---

3030 dev_err(&ctlr->dev, "queue restart failed\n");
3031
3032 return ret;
3033}
3034EXPORT_SYMBOL_GPL(spi_controller_resume);
3035
3036/*-------------------------------------------------------------------------*/
3037
906static void spi_set_cs(struct spi_device *spi, bool enable, bool force)
907{
908 bool activate = enable;
909
910 /*
911 * Avoid calling into the driver (or doing delays) if the chip select
912 * isn't actually changing from the last time this was called.
913 */

--- 2202 unchanged lines hidden (view full) ---

3116 dev_err(&ctlr->dev, "queue restart failed\n");
3117
3118 return ret;
3119}
3120EXPORT_SYMBOL_GPL(spi_controller_resume);
3121
3122/*-------------------------------------------------------------------------*/
3123
3038/* Core methods for SPI resource management */
3039
3040/**
3041 * spi_res_alloc - allocate a spi resource that is life-cycle managed
3042 * during the processing of a spi_message while using
3043 * spi_transfer_one
3044 * @spi: the spi device for which we allocate memory
3045 * @release: the release code to execute for this resource
3046 * @size: size to alloc and return
3047 * @gfp: GFP allocation flags
3048 *
3049 * Return: the pointer to the allocated data
3050 *
3051 * This may get enhanced in the future to allocate from a memory pool
3052 * of the @spi_device or @spi_controller to avoid repeated allocations.
3053 */
3054void *spi_res_alloc(struct spi_device *spi,
3055 spi_res_release_t release,
3056 size_t size, gfp_t gfp)
3057{
3058 struct spi_res *sres;
3059
3060 sres = kzalloc(sizeof(*sres) + size, gfp);
3061 if (!sres)
3062 return NULL;
3063
3064 INIT_LIST_HEAD(&sres->entry);
3065 sres->release = release;
3066
3067 return sres->data;
3068}
3069EXPORT_SYMBOL_GPL(spi_res_alloc);
3070
3071/**
3072 * spi_res_free - free an spi resource
3073 * @res: pointer to the custom data of a resource
3074 *
3075 */
3076void spi_res_free(void *res)
3077{
3078 struct spi_res *sres = container_of(res, struct spi_res, data);
3079
3080 if (!res)
3081 return;
3082
3083 WARN_ON(!list_empty(&sres->entry));
3084 kfree(sres);
3085}
3086EXPORT_SYMBOL_GPL(spi_res_free);
3087
3088/**
3089 * spi_res_add - add a spi_res to the spi_message
3090 * @message: the spi message
3091 * @res: the spi_resource
3092 */
3093void spi_res_add(struct spi_message *message, void *res)
3094{
3095 struct spi_res *sres = container_of(res, struct spi_res, data);
3096
3097 WARN_ON(!list_empty(&sres->entry));
3098 list_add_tail(&sres->entry, &message->resources);
3099}
3100EXPORT_SYMBOL_GPL(spi_res_add);
3101
3102/**
3103 * spi_res_release - release all spi resources for this message
3104 * @ctlr: the @spi_controller
3105 * @message: the @spi_message
3106 */
3107void spi_res_release(struct spi_controller *ctlr, struct spi_message *message)
3108{
3109 struct spi_res *res, *tmp;
3110
3111 list_for_each_entry_safe_reverse(res, tmp, &message->resources, entry) {
3112 if (res->release)
3113 res->release(ctlr, message, res->data);
3114
3115 list_del(&res->entry);
3116
3117 kfree(res);
3118 }
3119}
3120EXPORT_SYMBOL_GPL(spi_res_release);
3121
3122/*-------------------------------------------------------------------------*/
3123
3124/* Core methods for spi_message alterations */
3125
3126static void __spi_replace_transfers_release(struct spi_controller *ctlr,
3127 struct spi_message *msg,
3128 void *res)
3129{
3130 struct spi_replaced_transfers *rxfer = res;
3131 size_t i;

--- 1149 unchanged lines hidden ---
3124/* Core methods for spi_message alterations */
3125
3126static void __spi_replace_transfers_release(struct spi_controller *ctlr,
3127 struct spi_message *msg,
3128 void *res)
3129{
3130 struct spi_replaced_transfers *rxfer = res;
3131 size_t i;

--- 1149 unchanged lines hidden ---