One way to achieve maximum affinity is to modify weapons or armor at a workshop, as Codsworth "loves" this action. This is repeatable, but as of the latest patch, the cooldown period has dramatically increased from 3 to 48 hours for it to work again. However, adding an armor mod and adding a weapon mod are considered two separate actions, so one can do both.
Each action "loved" will add 35 points to the required 1000 for reaching maximum affinity, and with Codsworth starting at a base affinity of 0 it will require 29 weapon or armor modifications to be attached for maximum disposition.
Once "Codsworth idolizes you" appears as a notification, check in with Codsworth to begin the conversation needed to get the perk.